I can resolve json data in two formats;
One is normal, and the other is in the form of an array;
Common form:
The json data format returned by the server is as follows:
{"Userbean": {"Uid": "100196", "Showname": "\ u75af \ u72c2 \ u7684 \ u7334 \ u5b50", "Avtar": null, "State": 1 }}
The analysis code is as follows:
// TODO status processing 500 200
Int res = 0;
Res = httpClient.exe cute (httpPost). getStatusLine (). getStatusCode ();
If (res = 200 ){
/*
* Processing is performed when the return code is 200.
* Obtain and process the json data returned by the server.
**/
HttpResponse httpResponse = httpClient.exe cute (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 Arrays:
The data format returned by the server is:
{"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:
// TODO status processing 500 200
Int res = 0;
Res = httpClient.exe cute (httpPost). getStatusLine (). getStatusCode ();
If (res = 200 ){
/*
* Processing is performed when the return code is 200.
* Obtain and process the json data returned by the server.
**/
HttpResponse httpResponse = httpClient.exe cute (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 ());
/**
* Analyze the json format data returned by 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, you only need to use JSONObject in the normal form, and use JSONArray to convert it into a list.