Android USES Gson to parse JSON data in two ways

  • 2020-05-07 20:24:48
  • OfStack

Json is a universal data exchange format similar to XML, with higher transmission efficiency than XML.
Structurally, all data (data) can eventually be decomposed into three types:
The first type is a scalar (scalar), which is a single string (string) or number (numbers), such as the single word "Beijing".
The second type is sequence (sequence), in which a number of related data are placed in the order of 1, also known as array (array) or list (List), such as "Beijing, Shanghai".
The third type is the map (mapping), which is a name/value pair (Name/value), where the data has a name and a corresponding value, also known as a hash (hash) or dictionary (dictionary), such as "capital: Beijing".
The Json specification is very simple, requiring only a few hundred words per page, and Douglas Crockford claims that this specification never needs to be upgraded because it is specified.
1) the juxtaposed data is separated by a comma (", ").
2) the mapping is represented by a colon (" : ").
3) the collection (array) of juxtaposed data is represented by square brackets ("[]").
4) the set (object) of the map is represented by braces ("{}").

Gson can be used in Android to parse JSON data
First of all, from code. google. com/p/google - gson/downloads/download list GsonAPI:
google-gson-1.7.1-release.zip
Place gson-1.7.jar copy into libs(create a new libs folder in the project root directory).
The JSON data can be parsed using the following two methods:
Parse JSON data by obtaining JsonReader object:
 
String jsonData = "[{\"username\":\"arthinking\",\"userId\":001},{\"username\":\"Jason\",\"userId\":002}]"; 
try{ 
JsonReader reader = new JsonReader(new StringReader(jsonData)); 
reader.beginArray(); 
while(reader.hasNext()){ 
reader.beginObject(); 
while(reader.hasNext()){ 
String tagName = reader.nextName(); 
if(tagName.equals("username")){ 
System.out.println(reader.nextString()); 
} 
else if(tagName.equals("userId")){ 
System.out.println(reader.nextString()); 
} 
} 
reader.endObject(); 
} 
reader.endArray(); 
} 
catch(Exception e){ 
e.printStackTrace(); 
} 

Map JSON data into one object, and use fromJson() method of Gson object to get an array of one object for operation:
Create one POJO object User.java corresponding to JSON data:
 
public class User { 
private String username ; 
private int userId ; 
public String getUsername() { 
return username; 
} 
public void setUsername(String username) { 
this.username = username; 
} 
public int getUserId() { 
return userId; 
} 
public void setUserId(int userId) { 
this.userId = userId; 
} 
} 

Gson object is used to obtain User object data for corresponding operations:
 
Type listType = new TypeToken<LinkedList<User>>(){}.getType(); 
Gson gson = new Gson(); 
LinkedList<User> users = gson.fromJson(jsonData, listType); 
for (Iterator iterator = users.iterator(); iterator.hasNext();) { 
User user = (User) iterator.next(); 
System.out.println(user.getUsername()); 
System.out.println(user.getUserId()); 
} 

If the JSON string to be processed contains only one JSON object, you can directly use fromJson to get one User object:
 
String jsonData = "{\"username\":\"arthinking\",\"userId\":001}"; 
Gson gson = new Gson(); 
User user = gson.fromJson(jsonData, User.class); 
System.out.println(user.getUsername()); 
System.out.println(user.getUserId()); 

Related articles: