Two ways to parse JSON data using Gson in Android

Source: Internet
Author: User
Tags scalar tagname

JSON is an XML-like common data Interchange Format, which has higher transfer efficiency than XML; This article will introduce two methods of parsing JSON data, the need for friends can refer to the following

JSON is a common XML-like data interchange format that has a higher transfer efficiency than XML.
Structurally, all data can eventually be decomposed into three types:
The first type is scalar (scalar), which is a single string (string) or number (numbers), such as "Beijing", a separate word.
The second type is the sequence (sequence), which is a number of related data that are tied together in a certain order, also called an array or list, such as "Beijing, Shanghai."
The third type is mapping (mapping), a name/value pair (Name/value), which has a name and a value corresponding to it, also known as hash (hash) or dictionary (dictionary), such as "Capital: Beijing".
JSON specifications are very simple, with only one page hundreds of words can be said clearly, and Douglas Crockford claims that this specification never need to upgrade, because the provisions of the provision.
1) The data is separated by commas (",").
2) The map is represented by a colon (":").
3) a set (array) of side data is expressed in square brackets ("[]").
4) The Set of Mappings (objects) are represented by curly braces ("{}").

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
Copy the Gson-1.7.jar to Libs (the project root directory creates a new Libs folder).
You can use the following two methods to parse JSON data:
Parse the JSON data by getting the Jsonreader object:

Copy CodeThe code is 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, use the Fromjson () method of the Gson object to get an array of objects to manipulate:
Create a Pojo object corresponding to the 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 get the user object data for the appropriate operation:

Copy CodeThe code is 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 you are working with contains only one JSON object, you can use Fromjson to get a user object directly:

Copy CodeThe code is 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 ());

Two ways to parse JSON data using Gson in Android

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.