Two ways to parse JSON data using Gson in Android _android

Source: Internet
Author: User
Tags scalar tagname
JSON is a common XML-like data interchange format that has a higher transmission efficiency than XML.
Structurally, all data can eventually be decomposed into three types:
The first type is a scalar (scalar), a separate string or number (numbers), such as "Beijing", a separate word.
The second type is the sequence (sequence), in which several related data are tied together in a certain order, also called an array or list, such as "Beijing, Shanghai".
The third type is the mapping (mapping), a name/value pair (Name/value), where the data has a name and a corresponding value, which is also known as a hash (hash) or a dictionary (dictionary), such as "Capital: Beijing".
The specification of JSON is very simple, with only one page of hundreds of words to be clear, and Douglas Crockford claims that this specification never need to upgrade, because the provisions of the rules.
1 The data is separated by commas (",").
2) The mapping is represented by a colon (":").
3 the Set (array) of parallel data is represented by square brackets ("[]").
4 the Mapped collection (object) is represented by braces ("{}").

You can use Gson to parse JSON data in Android
First, download Gsonapi from Code.google.com/p/google-gson/downloads/list:
Google-gson-1.7.1-release.zip
Gson-1.7.jar Copy to Libs (create a new Libs folder in the project root directory).
You can use the following two methods to parse JSON data:
To parse the JSON data by getting the Jsonreader object:
Copy Code code as follows:

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 ();
}

By mapping the JSON data to an object, you use the Fromjson () method of the Gson object to get an array of objects to operate:
Create a Pojo object corresponding to the JSON data User.java:
Copy Code code 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 get the user object data for the appropriate action:
Copy Code code as follows:

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 get a user object directly using Fromjson:
Copy Code code as follows:

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 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.