Two Methods for parsing JSON data using Gson in Android

Source: Internet
Author: User

Json is a common data exchange format similar to XML, which has higher transmission efficiency than XML.
In terms of structure, all data can be divided into three types:
The first type is scalar, which is a separate string or number, such as the word "Beijing.
The second type is sequence, that is, several related data are grouped together in a certain order, also called array or List, for example, "Beijing, shanghai ".
The third type is mapping, that is, a Name/value pair (Name/value). That is, the data has a Name and a corresponding value, this is also called hash or dictionary, for example, "capital: Beijing ".
The Json specification is very simple. It can be clearly stated in just a few hundred words on a page, and Douglas Crockford claims that this specification will never need to be upgraded, because it is stipulated in this provision.
1) Separate the parallel data with commas.
2) The ing is represented by a colon.
3) The set (array) of the parallel data is represented by square brackets.
4) The ing set (object) is represented by braces.

In Android, Gson can be used to parse JSON data
First, download gsonapifrom code.google.com/p/google-gson/downloads/list:
Google-gson-1.7.1-release.zip
Copy the gson-1.7.jar to libs (the project root directory creates a new libs folder.
You can use either of the following methods to parse JSON data:
Parse JSON data by getting the JsonReader object:Copy codeThe Code is as follows: String jsonData = "[{\" username \ ": \" Arthur inking \ ", \" 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 ();
}

By ing JSON data into an object, you can use the fromJson () method of the Gson object to obtain an object array for operations:
Create a POJO object corresponding to JSON data User. java:Copy codeThe Code is as follows: 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;
}
}

Use the Gson object to obtain User object data for corresponding operations:Copy codeThe Code is as follows: Type listType = new TypeToken <shortlist <User> () {}. getType ();
Gson gson = new Gson ();
Jsonlist <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 obtain a User object:Copy codeThe Code is as follows: String jsonData = "{\" username \ ": \" Arthur inking \ ", \" userId \ ": 001 }";
Gson gson = new Gson ();
User user = gson. fromJson (jsonData, User. class );
System. out. println (user. getUsername ());
System. out. println (user. getUserId ());

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.