Android parsing JSON format data implementation code

Source: Internet
Author: User
Tags json readline

Ordinary form of:
The JSON data returned by the server side is formatted as follows:

The code is as follows Copy Code

{"Userbean": {"Uid": "100196", "ShowName": "U75afu72c2u7684u7334u5b50", "Avtar": null, "state": 1}}

The analysis code is as follows:

The code is as follows Copy Code

TODO State Processing 500 200
int res = 0;
res = Httpclient.execute (httppost). Getstatusline (). Getstatuscode ();
if (res = = 200) {
/*
* When the return code is 200, do the processing
* Get the server side to return JSON data and do the processing
* */
HttpResponse HttpResponse = Httpclient.execute (HttpPost);
StringBuilder builder = new StringBuilder ();
BufferedReader bufferedReader2 = new BufferedReader (
New InputStreamReader (Httpresponse.getentity (). GetContent ()));
String str2 = "";
for (String s = bufferedreader2.readline (); s!= null; s = bufferedReader2
. ReadLine ()) {
Builder.append (s);
}
LOG.I ("Cat", ">>>>>>" + builder.tostring ());

Jsonobject jsonobject = new Jsonobject (builder.tostring ())
. Getjsonobject ("Userbean");

String Uid;
String ShowName;
String Avtar;
String State;

UID = jsonobject.getstring ("UID");
ShowName = jsonobject.getstring ("ShowName");
Avtar = jsonobject.getstring ("Avtar");
State = jsonobject.getstring ("state");

With array form:
The data format returned by the server side is:

  code is as follows copy code
{"Calendar":
    {"calendarlist":
             [
             {"calendar_id": "1705", "title": "(U4EB2U5B50) DDSSD", "Category_name": "u9ed8u8ba4u5206u7c7b", "Showtime ":" 1288927800 "," Endshowtime ":" 1288931400 "," AllDay ": false},
             {"calendar_id": "1706", "title": "(u65c5u884c)", "Category_name": "u9ed8u8ba4u5206u7c7b", " Showtime ": 1288933200", "Endshowtime": "1288936800", "AllDay": false}
            ]
   }

The analysis code is as follows:

The code is as follows Copy Code
TODO State Processing 500 200
int res = 0;
res = Httpclient.execute (httppost). Getstatusline (). Getstatuscode ();
if (res = = 200) {
/*
* When the return code is 200, do the processing
* Get the server side to return JSON data and do the processing
* */
HttpResponse HttpResponse = Httpclient.execute (HttpPost);
StringBuilder builder = new StringBuilder ();
BufferedReader bufferedReader2 = new BufferedReader (
New InputStreamReader (Httpresponse.getentity (). GetContent ()));
String str2 = "";
for (String s = bufferedreader2.readline (); s!= null; s = bufferedReader2
. ReadLine ()) {
Builder.append (s);
}
LOG.I ("Cat", ">>>>>>" + builder.tostring ());
/**
* Here you need to analyze the JSON format data from the server.
*/
Jsonobject jsonobject = new Jsonobject (builder.tostring ())
. Getjsonobject ("Calendar");
Jsonarray Jsonarray = Jsonobject.getjsonarray ("Calendarlist");
for (int i=0;i<jsonarray.length (); i++) {
Jsonobject jsonObject2 = (jsonobject) jsonarray.opt (i);
Calendarinfo calendarinfo = new Calendarinfo ();
CALENDARINFO.SETCALENDAR_ID (jsonobject2.getstring ("calendar_id"));
Calendarinfo.settitle (jsonobject2.getstring ("title"));
Calendarinfo.setcategory_name (jsonobject2.getstring ("Category_name"));
Calendarinfo.setshowtime (Jsonobject2.getstring ("Showtime"));
Calendarinfo.setendtime (jsonobject2.getstring ("Endshowtime"));
Calendarinfo.setallday (Jsonobject2.getboolean ("AllDay"));
Calendarinfos.add (Calendarinfo);
}

To sum up, the ordinary form of simply using Jsonobject, with array form needs to use Jsonarray to turn it into a list.

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.