Android parses JSON Data Format

Source: Internet
Author: User
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 }}

AnalysisCodeAs follows:

// Todo status handling 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 = Httpclient.exe cute (httppost );
Stringbuilder =   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= NewJsonobject (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 handling 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 = Httpclient.exe cute (httppost );
Stringbuilder =   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 =   New Jsonobject (builder. tostring ())
. Getjsonobject ( " Calendar " );
Jsonarray = Jsonobject. getjsonarray ( " Calendarlist " );
For ( Int I = 0 ; I < Jsonarray. Length (); I ++ ){
Jsonobject jsonobject2 = (Jsonobject) jsonarray. Opt (I );
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.

 

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.