JSON data format and parsing

Source: Internet
Author: User

First, JSON (JavaScript Object Notation) is a lightweight data interchange format. Easy for people to read and write.  It is also easy for machine parsing and generation. JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, C #, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language.

Second, Two types of JSON structure:

1. The collection of "name/value" pairs.

2, an ordered list of values, commonly called arrays.

The JSON has these forms:

An object is an unordered collection of "name/value pairs". An object starts with "{" (opening parenthesis) and "}" (the closing parenthesis) ends. Each "name" is followed by a ":" (colon); "' Name/value ' pair ' is separated by", "(comma).

An array is an ordered collection of values (value). An array begins with "[" (the left square bracket), and "]" (the right square bracket) ends. Use "," (comma) to separate values.

The value(value) can be a string enclosed in double quotation marks (string), a numeric value (number), A,,, true object, false null or array. These structures can be nested.

A stringis a collection of any number of Unicode characters surrounded by double quotation marks, escaped with a backslash. A character (character) is a separate string (character string).

A stringis very similar to a C or Java string.

The value(number) is also very similar to the value of C or Java. Removes octal and hexadecimal formats that have not been used. Remove some coding details.

Whitespace can be added to any symbol.

Above –json.org.

Third, the complex structure of JSON.

Here is a website that returns data as follows:

{"Showapi_res_code": 0, "Showapi_res_error": "", "Showapi_res_body": {"list": [{"P0": "6.07", "P90": "6.4", "p93": "6.5", "p97": "6.94", "Prov": "Liaoning"}], "Ret_code": 0}}

The data structure is as follows

This data is generated in Java to better understand the data structure of JSON.

The code is as follows:

First define a Prov class:

 Packagecom.example.test; Public classprov{PrivateString P0, P90, p93, P97,prov;//The name of each variable corresponds to the name of the JSON data the Web site obtains, otherwise it cannot parse the data correctly. The resulting data format is the same, but the name/value does not have the same name.      PublicString Getprov () {returnProv; }     Public voidSetprov (String prov) { This. Prov =Prov; }     PublicString getP0 () {returnP0; }     Public voidsetP0 (String p0) { This. P0 =P0; }     PublicString getP90 () {returnP90; }     Public voidsetP90 (String P90) { This. P90 =P90; }     PublicString getP93 () {returnp93; }     Public voidsetP93 (String p93) { This. p93 =p93; }     PublicString getP97 () {returnp97; }     Public voidsetP97 (String p97) { This. p97 =p97; }}

The body class on the previous layer:

 Packagecom.example.test;Importjava.util.List; Public classbody{PrivateString Ret_code; PrivateList<com.example.test.prov> list;//here the name is in list, and the JSON data obtained by the website corresponds to the name. Otherwise the format produces the same data format, but the name is the same. When parsing JSON data, it is not parsed correctly.      PublicString Getret_code () {returnRet_code; }     Public voidSetret_code (String ret_code) { This. Ret_code =Ret_code; }     PublicList<com.example.test.prov>Getprov () {returnlist; }     Public voidSetprov (list<com.example.test.prov>Prov) {         This. List =Prov; }}

Another layer of data Listdata class

 Packagecom.example.test; Public classlistdata{//as in the above two, the name of the defined variable is consistent with the JSON.     PrivateString Showapi_res_code,showapi_res_error; PrivateBody Showapi_res_body;  PublicString Getshowapi_res_code () {returnShowapi_res_code; }     Public voidSetshowapi_res_code (String showapi_res_code) { This. Showapi_res_code =Showapi_res_code; }     PublicString Getshowapi_res_error () {returnShowapi_res_error; }     Public voidsetshowapi_res_error (String showapi_res_error) { This. Showapi_res_error =Showapi_res_error; }     PublicBody getshowapi_res_body () {returnShowapi_res_body; }     Public voidsetshowapi_res_body (Body showapi_res_body) { This. Showapi_res_body =Showapi_res_body; }    }

Best to work with JSON data in main

 Packagecom.example.test;Importjava.util.ArrayList;Importjava.util.List;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.Window;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;ImportCom.google.gson.Gson;Importcom.show.api.ShowApiRequest; Public classMainactivityextendsActivityImplementsonclicklistener{PrivateButton btn; PrivateTextView TextView; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title);        Setcontentview (R.layout.activity_main); TextView=(TextView) Findviewbyid (R.id.textview); BTN=(Button) Findviewbyid (R.ID.BTN); Btn.setonclicklistener ( This); String Res=NewShowapirequest ("http://route.showapi.com/138-46", "1255", "9b8cc3f4086048b8a506d731d1af0a9c"). Addtextpara ("Prov", "Liaoning"). Post ();        System.out.println (RES); //------------start with the object---JSON dataCom.example.test.Prov Prov=NewCom.example.test.Prov (); Prov.setp0 ("6.07"); Prov.setp90 ("6.4"); Prov.setp93 ("6.5"); PROV.SETP97 ("6.97"); Prov.setprov (Liaoning); List<com.example.test.Prov> provlist =NewArraylist<com.example.test.prov>();        Provlist.add (PROV); Body Body=NewBody (); Body.setret_code ("0");        Body.setprov (provlist); ListData ListData=NewListData ();        Listdata.setshowapi_res_body (body); Listdata.setshowapi_res_error (""); Listdata.setshowapi_res_code ("0"); Gson Gson=NewGson (); String s=Gson.tojson (ListData);        System.out.println (s); //---------------End of the object's specially-JSON data//----------------parsing JSON data---startListData Listdatas = Gson.fromjson (res, ListData.class); String Showapi_res_code=Listdatas.getshowapi_res_code (); String Showapi_res_error=Listdatas.getshowapi_res_error (); Body Body2=Listdatas.getshowapi_res_body (); System.out.println ("Showapi_res_code =" + Showapi_res_code + "" + "Showapi_res_error =" + Showapi_res_error + " Ret_code= "+Body2.getret_code ()); List<Prov> PROV2 =Body2.getprov ();  for(Prov prov1:prov2) {System.out.println (prov1.getp93 ()+ "       " +Prov1.getprov ()); }        //----------------------parsing the end of JSON data} @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.btn:textview.settext (A);  Break; default:             Break; }    }}

JSON data format and parsing

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.