1.JSON Concept:
A lightweight data interchange format that provides good readability and quick-to-write features that enable data exchange between different platforms. JSON uses a high-compatibility text format, and also has a similar behavior to the C language system.
JSON can convert a Java object into a JSON-formatted string that converts a JSON string into Java. More lightweight than XML, JSON is lightweight and simple to use. JSON data format, which is widely used in Android for client and server communication, is very convenient for network data transmission and parsing.
2. Environment configuration
Http://code.google.com/p/google-gson/has a class library called Gson, which can be used to parse JSON data, and the Adroid 3.0 platform actually integrates this part directly into Android. We will parse the JSON data, go directly to the site to download a jar package, import into the project, you can parse the JSON data.
3.JSON applications
(1). JSON is a lightweight Data Interchange Format (2). JSON is based on two types of data structures: Object and array. Where object is a collection of "name/value" pairs.
(3) contains four JSON-related classes and one exceptions in Android:
A.jsonobject
This is the basic unit in the system for JSON definition, which contains a pair of key/value values.
B.jsonarray
It represents an ordered set of values. Converts it to a string output (toString) in the form of a square bracket wrapped with a value separated by a comma "," (for example: [Value1,value2,value3]
C.jsonstringer
This class can help create jsontext quickly and easily. Its greatest advantage is that it can reduce program exceptions due to format errors, which can automatically create JSON text in strict accordance with JSON syntax rules (syntaxrules). Each Jsonstringer entity can only create one JSON text. For example:
New Jsonstringer (). Object (). Key ("name"). value ("Piglet"). EndObject ()
myString ={"name": "Piglet"}
D.jsontokener
This is the class for the system to parse the JSON source string for the Jsonobject and Jsonarray constructors, which can extract numeric information from the source string.
E.jsonexception
(4) JSON format example
A.object Example:
{ "Image": { "Width": +, "Height": +, "Title": " View from 15th Floor ", " Thumbnail ": { " Url ": " http://www.example.com/image/ 481989943 ", " Height ": " Max "," Width ":" + " }, " IDs ": [ 943, 234, 38793] }}
B.array Example :
[ { "Precision": "Zip", "Latitude": 37.7668, "Longitude":-122.3959, "Address": "", "City": "SAN FRANCISCO", "State": "CA", "Zip": "94107", "Country": "US" }, { "Precision": "Zip", "Latitude": 37.371991, "Longitude":-122.026020, "Address": "", "City": "SUNNYVALE", "State": "CA", "Zip": "94085", "Country": "US" }]
4.JSON Analytic case
(1) Parse one of the object:
String jsonstring ={"url": "Http://www.cnblogs.com/qianxudetianxia"new= demojson.getstring ("url");
(2) Parse object two:
String jsonstring ={"name": "Android", "Version": "Beta1.0"new= demojson.getstring ("name" = demojson.getstring ("version"); System.out.println ("Name:" +name+ ", Version:" +version);
(3). Parse one of the array:
String jsonstring ={"number": [All-in-all]}; New = Demojson.getjsonarray ("number"for (int i=0; i<numberlist.length (); i++ { // because the type in the array is int, so for Getint, the other getstring,getlong System.out.println ( Numberlist.getint (i));}
(4). Parse the second of array:
String jsonstring ={"number": [[[1],[2],[3]]}; Parsing method:// nested array traversal new= Demojson.getjsonarray ("number"); for (int i=0; i<numberlist.length (); i++) { // gets the array in the array System.out.println (Numberlist.getjsonarray (i). GetInt (0));}
(5). parsing Object and the Array:
String jsonstring ={"mobile": [{"Name": "Android"},{"name": "iphone"new= Demojson.getjsonarray ("mobile"); for (int i=0; i<numberlist.length (); i++) { System.out.println (Numberlist.getjsonobject (i). GetString ("name"));}
(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.
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 of String url = demojson.optstring ("url");
(7). UTF-8 BOM header causes the problem of parsing JSON exceptions to JSON files saved as Utf-8, under the Windows platform, The BOM Header "ef BB ef" bytes will be generated at the front of the text (required to be opened with the 16 binary tool to see). There are two solutions: a. Using UltraEdit to open the JSON file, save as the time, choose the format UTF-8, no BOM head, if not yet, in Notepad open, save as UTF-8, more than a few times to try. B. Use code processing to intercept the contents of the JSON body:
String jsonstring = getjsonstring ();
Jsonstring =jsonstring.substring (Jsonstring.indexof ("{"), Jsonstring.lastindexof ("}") +1);
Android Data format parsing object JSON usage