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.