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:

String Jsondata ="[{\ "username\": \ "arthinking\", \ "userid\": 001},{\ "username\": \ "jason\", \ "userid\": 002}]";Try{Jsonreader Reader=NewJsonreader (NewStringReader (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:

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

New typetoken<linkedlist<user>>new  Gson (); LinkedList<User> users = Gson.fromjson (jsondata, listtype);  for (Iterator Iterator == (User) iterator.next (); System.  out . println (User.getusername ()); System.  out

If the JSON string you are working with contains only one JSON object, you can use Fromjson to get a user object directly:

" {\ "username\": \ "arthinking\", \ "userid\": 001} "  new= Gson.fromjson (Jsondata, User.  Class); System.  out . println (User.getusername ()); System.  out

Two ways to parse JSON data using Gson in Android

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.