Getting Started with Android (22) parsing JSON

Source: Internet
Author: User

Original link: http://www.orlion.ga/687/

There are many ways to parse JSON, mainly the official jsonobject, Google's Open Source Library Gson. In addition, some third-party open source libraries such as Jackson, Fastjson and so on are also very good.

Suppose the JSON data is:

[{"id": "5", "Version": "5.5", "name": "Angry Birds"},{"id": "6", "Version": "7.0", "Name": "Clash of Clans"},{"id": "7", " Version ":" 3.5 "," name ":" Hey Day "}]

First, Jsonobject

try {    jsonarray jsonarray =  New jsonarray (Jsondata);    for  (int i = 0; i <  jsonarray.length ();  i++)  {    JSONObject jsonObject =  Jsonarray.getjsonobject (i);     string id = jsonobject.getstring ("id");     string name = jsonobject.getstring ("name");     String version = jsonobject.getstring ("version"); &NBSP;&NBSP;&NBSP;&NBSP;LOG.D ("MainActivity",   "id is "  + id)     log.d ("mainactivity",  "Name is   " + name" &NBSP;&NBSP;&NBSP;&NBSP;LOG.D ("mainactivity",  "version is "  +  version);}}  catch  (exception e)  {    e.printstacktrace ();} 

The first is to pass the data returned by the server into a Jsonarray object. The Jsonarray is then iterated through, and each element taken from it is a Jsonobject object, and each Jsonobject object contains the data for the ID, name, and version. Next, just call the GetString () method to remove the data and print it out.

Second, the use of Gson

It is mainly the ability to automatically map a JSON-formatted string into an object, such as a JSON-formatted data as follows:

{"Name": "Tom", "Age": 20}

Then we can define a person class and add the name and age fields, and then simply call the following code to automatically parse the JSON data into a person object:

Gson Gson = new Gson (); Person person = Gson.fromjson (Jsondata, Person.class);

If it is a bit of a hassle to parse a JSON array, we need to pass the expected parsed data type into the Fromjson () method with TypeToken, as shown here:

list<person> people = Gson.fromjson (Jsondata, New typetoken<list<person>> () {}.getType ());

For the JSON data at the beginning of this article, test Gson and first create the class app:

public class app {    private string id;     private string name;    private string version;     public string getid ()  {        return id;     }    public void setid (String id)  {         this.id = id;    }     public string getname ()  {        return name ;     }    public void setname (String name)  {         this.name = name;    }     public string getversion ()  {        return  version;     }    public void setversion (string version)  {         this.version = version;    }}

Analytical:

Gson Gson = new Gson (); list<app> applist = Gson.fromjson (Jsondata, newtypetoken<list<app>> () {}.getType ()); for (app:    Applist) {log.d ("mainactivity", "ID is" + app.getid ());    LOG.D ("Mainactivity", "name is" + app.getname ()); LOG.D ("Mainactivity", "version is" + app.getversion ());}

Getting Started with Android (22) parsing JSON

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.