You don't know the efficient usage of JSON, the efficient usage of json
1. JSON
JSON is the abbreviation of JavaScript Object Notation and a subset of JavaScript standards. The official Android API has built-in support for reading and writing JSON data. This format is suitable for complex objects that do not contain binary data. To some extent, it has become a de facto standard for data sharing on the network.
The following example shows a simple JSON array, which contains three objects, each of which stores People information. This format is suitable for sending tasks on network services or sharing data directly among friends.
[
{
"Name": "liyuanjinglyj ",
"Age": "22 ",
"Lon": "12"
},
{
"Name": "fengxinyao ",
"Age": "24 ",
"Lon": "22"
},
{
"Name": "hefan ",
"Age": "23 ",
"Lon": "11"
}
]
JsonReader API is recommended for reading JSON data from InputStream, as shown below:
public JSONArray readPeopleFromInputStream(InputStream inputStream){ InputStreamReader reader=new InputStreamReader(inputStream); JsonReader jsonReader=new JsonReader(reader); JSONArray jsonArray=new JSONArray(); try { jsonReader.beginArray(); while(jsonReader.hasNext()){ JSONObject jsonObject=readSingleJSON(jsonReader); jsonArray.put(jsonObject); } jsonReader.endArray(); } catch (Exception e) { e.printStackTrace(); } return jsonArray;}private JSONObject readSingleJSON(JsonReader jsonReader)throws Exception{ JSONObject jsonObject=new JSONObject(); jsonReader.beginObject(); JsonToken token; do{ String name=jsonReader.nextName(); if("name".equals(name)){ jsonObject.put("name",jsonReader.nextString()); }else if("age".equals(name)){ jsonObject.put("age",jsonReader.nextString()); }else if("lon".equals(name)){ jsonObject.put("lon",jsonReader.nextString()); } token=jsonReader.peek(); }while(token!=null&&!token.equals(JsonToken.END_OBJECT)); jsonReader.endObject(); return jsonObject;}
Although all the content in InputStream can be read to the String and passed to the JSONArray constructor, the previous method consumes less memory and may very quickly. Similarly, the JsonWriter class allows OutputStream to efficiently write JSON data, as shown below:
public void writePeopleJSON(JSONArray jsonArray,OutputStream outputStream) throws Exception { OutputStreamWriter write=new OutputStreamWriter(outputStream); JsonWriter jsonWrite=new JsonWriter(write); int arrayLength=jsonArray.length(); jsonWrite.beginArray(); for (int i = 0; i < arrayLength; i++) { JSONObject jsonObject= (JSONObject) jsonArray.get(i); jsonWrite.beginObject(); jsonWrite.name("name").value(jsonObject.getString("name")); jsonWrite.name("age").value(jsonObject.getString("age")); jsonWrite.name("lon").value(jsonObject.getString("lon")); jsonWrite.endObject(); } jsonWrite.endArray(); jsonWrite.close();}
2. Use Gson for advanced JSON Processing
JSONObject and JSONArray classes are easy to use, but they have certain limitations and usually consume more unnecessary memory. Similarly, if there are multiple different types of objects, using JsonReader and JsonWriteer requires a considerable amount of code. For more advanced JSON data serialization and deserialization methods, you can use the excellent open-source library Gson.
Gson allows you to convert a simple Java Object (Plain Old Object, POJO) to JSON, and vice versa. All developers need to do is define data as common Java objects, provide get and set methods, and introduce the Gson library into the project.
The following class shows a simple Java object that represents a task:
public class People { private String name; private String age; private String lon; public void setName(String name) { this.name = name; } public void setAge(String age) { this.age = age; } public void setLon(String lon) { this.lon = lon; } public String getName() { return name; } public String getAge() { return age; } public String getLon() { return lon; }}
The following code shows how to read and write Collection <People> object sets. The serialization form is always valid JSON data, so it is convenient to select it when publishing JSON data to Web Services. If developers are also responsible for the server-side code and use the Java language, they can easily share the same set of Java code between the server and Android applications.
public Collection<People> readPeopleFromStream(InputStream inputStream){ InputStreamReader reader=new InputStreamReader(inputStream); Gson gson=new Gson(); Type type=new TypeToken<Collection<People>>(){}.getType(); return gson.fromJson(reader,type);}public void writePeopleToStream(Collection<People> peoples,OutputStream outputStream) throws IOException { OutputStreamWriter write=new OutputStreamWriter(outputStream,"UTF-8"); com.google.gson.stream.JsonWriter jsonWrite= new com.google.gson.stream.JsonWriter(write); Gson gson=new Gson(); Type type=new TypeToken<Collection<People>>(){}.getType(); gson.toJson(peoples,type,jsonWrite); Log.i("MainActivity", "GSON"); jsonWrite.flush(); jsonWrite.close();}
This article also provides the latest GSON package for programmers to download. Please refer to the attachment. GSON Development Kit