JSON data format parsing I divide myself into two kinds;
One is ordinary, one is with array form;
Ordinary form of:
The JSON data returned by the server side is formatted as follows:
Copy Code code as follows:
{"Userbean": {"Uid": "100196", "ShowName": "\u75af\u72c2\u7684\u7334\u5b50", "Avtar": null, "state": 1}}
The analysis code is as follows:
Copy Code code as follows:
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:
Copy Code code as follows:
{"Calendar":
{"Calendarlist":
[
{"calendar_id": "1705", "title": "(\U4EB2\U5B50) DDSSD", "Category_name": "\u9ed8\u8ba4\u5206\u7c7b", "Showtime": " 1288927800 "," Endshowtime ":" 1288931400 "," AllDay ": false},
{"calendar_id": "1706", "title": "(\u65c5\u884c)", "Category_name": "\u9ed8\u8ba4\u5206\u7c7b", "Showtime": " 1288933200 "," Endshowtime ":" 1288936800 "," AllDay ": false}
]
}
}
The analysis code is as follows:
Copy Code code as follows:
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.