Two ways to parse JSON data using Gson in Android

Source: Internet
Author: User
Tags scalar

JSON is an XML-like common data Interchange Format, which has higher transfer efficiency than XML; This article will introduce two methods to parse JSON data, the need for friends can refer to JSON is a common XML-like data Interchange format, with higher 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: The code looks like this:
1String Jsondata ="[{\ "username\": \ "arthinking\", \ "userid\": 001},{\ "username\": \ "jason\", \ "userid\": 002}]"; 2 Try{ 3Jsonreader reader =NewJsonreader (NewStringReader (jsondata)); 4 Reader.beginarray ();5  while(Reader.hasnext ()) {6 Reader.beginobject ();7  while(Reader.hasnext ()) {8String TagName =reader.nextname ();9 if(Tagname.equals ("username")){ TenSystem. out. println (Reader.nextstring ()); One }  A Else if(Tagname.equals ("userId")){  -System. out. println (Reader.nextstring ()); - }  the }  - Reader.endobject (); - }  - Reader.endarray (); + }  - Catch(Exception e) { + e.printstacktrace (); A}

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:

The code is as follows:

1  Public classUser {2 PrivateString username;3 Private intuserId;4  PublicString GetUserName () {5 returnusername;6 } 7  Public voidSetusername (String username) {8  This. Username =username;9 } Ten  Public intgetUserId () { One returnuserId; A }  -  Public voidSetuserid (intuserId) {  -  This. UserId =userId; the }  -}

Use the Gson object to get the user object data for the appropriate operation:

The code is as follows:

1 " {\ "username\": \ "arthinking\", \ "userid\": 001} "  2new3 User user = Gson.fromjson (jsondata, user.  Class4 System.  out  5 System.  out

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.