Android: parse JSON using gon

Source: Internet
Author: User
Tags parse string tojson


Android provides JSON parsing, but its jsonreader SDK is available after 3.0.

Its resolution method can refer to the http://developer.android.com/reference/android/util/JsonReader.html

 

For earlier versions, we can use some additional packages to facilitate resolution.

Use gson package

Com. Google. gson. Stream. jsonreader

: Http://code.google.com/p/google-gson/downloads/list

DocumentDocs:Http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/index.html
For example, the data to be parsed is

String jsondata = "[{\" Name \ ": \" Michael \ ", \" Age \ ": 20 },{ \" Name \ ": \" Mike \", \ "Age \": 21}] ";

The parsing method can be as follows:

         Try  {  //  To parse JSON data, you must first generate a jsonreader object. Jsonreader reader = New Jsonreader ( New  Stringreader (jsondata); reader. beginarray ();  While  (Reader. hasnext () {reader. beginobject (); While  (Reader. hasnext () {string tagname = Reader. nextname ();  If (Tagname. Equals ("name" ) {System. Out. println ( "Name --->" + Reader. nextstring ());}  Else   If (Tagname. Equals ("Age" ) {System. Out. println ( "Age --->" +Reader. nextint ();} reader. endobject ();} reader. endarray ();}  Catch  (Exception e) {e. printstacktrace ();} 

 

Parses a JSON object to a custom objectFor example:

To parse string jsondata = "{\" Name \ ": \" Michael \ ", \" Age \ ": 20 }";

Users of the model class can be defined.

 Public   Class  User {  Private  String name;  Private  Int  Age;  Public  String getname (){  Return  Name ;}  Public   Void  Setname (string name ){  This . Name = Name ;}  Public   Int  Getage (){  Return Age ;}  Public   Void Setage ( Int  Age ){  This . Age = Age ;}} 

Resolution Process:

 
Gson =NewGson (); User user= Gson. fromjson (jsondata, user.Class); System. Out. println ("Name --->" +User. getname (); system. Out. println ("Age ---->" + User. getage ());


Supplement:The JSON data to be parsed is as follows:

 

 
{Alist: [{A1:1, A2:2, A3:3}, {A1:11, A2:22, A3:33}], Blist: [{B1:1, B2:2, B3:3}, {B1:11, B2:22, B3:33}], C:0}

 

Custom objects are as follows:

 

 Public class model {private list <A> Alist; private list <B> Blist; private  Int  C; public class A {private  Int  A1; private  Int  A2; private  Int  A3;...} public class B {private  Int  B1; private  Int B2; private  Int  B3 ;.........}........}  //  Get set is omitted. 

 

 

 

 

Parses a set of JSON objects to custom objects.

For example, the data to be parsed is:

String jsondata = "[{\" Name \ ": \" Michael \ ", \" Age \ ": 20 },{ \" Name \ ": \" Mike \", \ "Age \": 21}] ";

Resolved to the user, which is defined as above,

The parsing process is

     Public  Void  Parseuserfromjson (string jsondata) {type listtype = New Typetoken <shortlist <user> () {}. GetType (); gson = New  Gson (); writable list <User> Users = Gson. fromjson (jsondata, listtype );  //  Traverse users with iterator          For (Iterator = Users. iterator (); iterator. hasnext ();) {user =(User) iterator. Next (); system. Out. println ( "Name --->" + User. getname (); system. Out. println ( "Age ---->" + User. getage ());} 

And check it: http://stackoverflow.com/questions/2779251/convert-json-to-hashmap-using-gson-in-java

 

Object --> JSON

 

Assume that we have a class person:

 

Public class person {

Public long ID;

Public string name;

Public Boolean ismale;

Public String Avatar;

}

 

Use a gson instance:

 

Gson = new gson ();

 

We can directly convert a person instance into a JSON string:

 

Person = new person ();

Person. ID = 111;

Person. Name = "James ";

Person. ismale = true;

Person. Avatar = "http: // aaaaaaaaa ";

 

String jsonstr = gson. tojson (person );

Log. V ("================", jsonstr );

 

Even list and map can be used (taking list as an example ):

 

Arraylist <person> List = new arraylist <person> ();

 

Person = new person ();

Person. ID = 111;

Person. Name = "James ";

Person. ismale = true;

Person. Avatar = "http: // aaaaaaaaa ";

List. Add (person );

Person = new person ();

Person. ID = 222;

Person. Name = "Li Si ";

Person. ismale = false;

Person. Avatar = "http: // bbbbbbbbbbb ";

List. Add (person );

 

String jsonstr = gson. tojson (list );

Log. V ("================", list );

 

See: http://iandroiddev.com/post/2011-10-08/5647743

 

 

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.