Let's learn the JSON definition first:
(1). JSON is a lightweight format for data interchange
(2). JSON is based on two types of data structures: Object and array. Where object is a collection of "name/value" pairs.
(3). Object: Curly braces, each group of String-value are combined with "," delimited, string and value separated by colons.
JSON data format, in Android is widely used in the client and network (or server) communication, it is necessary to learn the system.
JSON parsing
(1). Parse one of the object:
{"url": "Http://zebra.com"}
parsing method:
<span style= "FONT-SIZE:18PX;" ><code class= "Java plain" >jsonobject Demojson = </code><code class= "java keyword" ><span style= "Color: #0000ff;" >new</span></code> <code class= "Java Plain" >jsonobject (jsonstring); </code></span><div class= "container" ><div class= "line number2 index1 alt1" ><span style= " font-size:18px; " ><code class= "java plain" >string url = demojson.getstring (</code><code class= "java String" >< Span style= "color: #0000ff;" > "url" </span></code><code class= "java plain" >); </code></span></div></ Div>
(2). Parse Object two:
<span style= "FONT-SIZE:18PX;" >{"name": "Android", "Name": "iphone"}</span>
parsing method:
<span style= "FONT-SIZE:18PX;" >jsonobject Demojson = new Jsonobject (jsonstring); String name = demojson.getstring ("name"); String Version = demojson.getstring ("version"); System.out.println ("Name:" +name+ ", Version:" +version);</span>
(3). Parse one of the array:
<span style= "FONT-SIZE:18PX;" >{"Number":[1,2,3]}</span>
parsing method:
<span style= "FONT-SIZE:18PX;" >jsonobject Demojson = new Jsonobject (jsonstring); Jsonarray numberlist = Demojson.getjsonarray ("number"); for (int i=0; i<numberlist.length (); i++) { // Because the type in the array is int, for GETINT, the other getstring,getlong are used System.out.println (Numberlist.getint (i));} </span>
(4). Parse the second of array:
<span style= "FONT-SIZE:18PX;" >{"Number":[[1],[2],[3]]}</span>
parsing method:
<span style= "FONT-SIZE:18PX;" >//nested arrays traverse jsonobject Demojson = new Jsonobject (jsonstring); Jsonarray numberlist = Demojson.getjsonarray ("number"), for (int i=0; i<numberlist.length (); i++) { //get array in array System.out.println (Numberlist.getjsonarray (i). GetInt (0));} </span>
(5). Parse object and array:
<span style= "FONT-SIZE:18PX;" >{"mobile": [{"Name": "Android"},{"name": "iphone"}]}</span>
parsing method:
<span style= "FONT-SIZE:18PX;" >jsonobject Demojson = new Jsonobject (jsonstring); Jsonarray numberlist = Demojson.getjsonarray ("mobile"); for (int i=0; i<numberlist.length (); i++) { System.out.println (Numberlist.getjsonobject (i). getString ("name")); </span>
So, we find that getting back is followed by the type of results you want: GetType, which is helpful for understanding.
(6). Use Opttype:
In the above example, the use of GetType throws an exception when it encounters a node that is not found.
If a node is not found with opttype, null or default value is returned.
<span style= "FONT-SIZE:18PX;" >//no URL node, throws an exception string url = demojson.getstring ("url"),//No URL node, returns NULL, if it is a base type, returns the default value string url = demojson.optstring (" URL ");</span>
The above is our usual JSON encapsulation method, the following is the code of my own project, backup:
<span style= "FONT-SIZE:18PX;" >jsonobject ob = new Jsonobject () ob.put ("list", list);//System.out.println (ob.tostring ()); Out.write (js.tostring ());//Finally, do not forget to flush the IF (out! = null) { out.flush; Out.close;} </span>
The general JSON used in the project is so much, very simple, mainly attention to extract from SQL data format problem, select * out of the data is not right, this is the case today.
Data processing JSON format and Java authoring considerations