The definition of JSON
A lightweight data interchange format with good readability and easy-to-write features. The industry's mainstream technology provides a complete solution (somewhat like regular expressions, which is supported in most languages today), enabling data exchange between different platforms. JSON uses a high-compatibility text format, and also has a similar behavior to the C language system.
JSON parsing classes provided by Android
The JSON parsing part of Android is under package Org.json , mainly in the following categories:
Jsonobject: Can be thought of as a JSON object, which is the basic unit in the system for json definition, which contains a pair of tostring () method output value key and value are colon ":" delimited). Its operation format for internal jsonobject instance, referencing the internal The put () method adds a value: New Jsonobject (). Put ("JSON", "Hello, world!"), between key and value is a comma Span lang= "en-us" > "," delimited. value types include: boolean, jsonarray, Jsonobject, number, string or default jsonobject.null object.
Jsonstringer:json text Building class, according to the official explanation, this class can help to create JSON text quickly and conveniently. The biggest advantage is that you can reduce program exceptions due to malformed formatting, and referencing this class automatically creates JSON text in strict accordance with the JSON syntax rules (syntax rules). Each Jsonstringer entity can only be created with one JSON text: The biggest advantage is that you can reduce program exceptions due to malformed formatting, and referencing this class automatically creates JSON text in strict accordance with the JSON syntax rules (syntax rules). Each Jsonstringer entity can only create one JSON text.
Jsonarray: It represents an ordered set of values. Converting it to a string output (toString) is performed in the form of a square bracket, separated by a comma "," (for example: [Value1,value2,value3], you can personally use the short code to understand its format more intuitively). The inside of this class also has query behavior, both get () and opt () can return the specified value through the index index, and the put () method is used to add or replace values. Similarly, the value type of this class can include: Boolean, Jsonarray, Jsonobject, number, string, or Default value Jsonobject.null object.
Jsontokener:json Parsing class
Exceptions used in Jsonexception:json
let's look at a simple example to see
1 Package Com.example.jsondemo;2 3 4Import java.util.*;5 6 import Org.json.JSONArray;7 import org.json.JSONException;8 import Org.json.JSONObject;9 Ten import android.app.Activity; One import Android.os.Bundle; A import Android.util.Log; - - the Public classMainactivity extends Activity { - - Private StaticFinal String Json ="[{name:\ "Zhang San \", age:12},{name:\ "calendar three \", age:15},{name:\ "smile \", age:32},{name:\ "thinking \", age:52}]"; - + @Override - protected voidonCreate (Bundle savedinstancestate) { + super.oncreate (savedinstancestate); A Setcontentview (r.layout.activity_main); at - Try { -Jsonarray JSON =NewJsonarray (Json); - for(inti =0; I < json.length (); i++) { -Jsonobject j = (Jsonobject) JSON.Get(i); -LOG.I ("Info","name="+j.getstring ("name")); inLOG.I ("Info","age="+j.getstring (" Age")); - } to}Catch(jsonexception e) { + //TODO auto-generated Catch block - e.printstacktrace (); the } * } $}
Because I am lazy, we use the layout to show him the number, directly through the log to print him out
Results of printing
Well, this concludes my study for today.
Android rookie growth 1--JSON parsing