Json reference
Json specification rfc4627: http://www.ietf.org/rfc/rfc4627.txt
Introduction to http://www.json.org/json-zh.html
Json entry reference: http://www.cnblogs.com/Truly/archive/2006/12/31/608896.html
Json parsing class provided by android2.3
The json parsing section of android is in the org. json package, which mainly includes the following classes:
JSONObject: it can be viewed as a json object.
JSONStringer: json Text Build class
JSONArray: json array.
JSONTokener: json parsing class
JSONException: Exceptions in json
JSONObject and JSONArray to build json text
// Assume that the following json text is to be created: // {// "phone": ["12345678", "87654321"], // array // "name ": "yuanzhifei89", // string // "age": 100, // value // "address": {"country": "china", "province ": "jiangsu"}, // object // "married": false // Boolean value //} try {// first the outermost layer is {}, is to create an object JSONObject person = new JSONObject (); // the value of the first key phone is an array, so you need to create an array object JSONArray phone = new JSONArray (); phone. put ("12345678 "). put ("87654321"); person. put ("phone", phone); person. put ("name", "yuanzhifei89"); person. put ("age", 100); // The Key address value is an object, so you have to create an object JSONObject address = new JSONObject (); address. put ("country", "china"); address. put ("province", "jiangsu"); person. put ("address", address); person. put ("married", false);} catch (JSONException ex) {// The Key is null or the NaN, infinities format is not supported by json) throw new RuntimeException (ex );}
Use of getType and optType APIs
GetType can convert the value of the key to be obtained to the specified type. If the key cannot be converted or there is no value, JSONException is thrown.
OptType is also the value of the key to be obtained to the specified type. If the key cannot be converted or no value is available, the value provided by the user or the default value is returned.
Try {// All used objects use the object created above // convert the first phone number to a value and the name to a value phone. getLong (0); person. getLong ("name"); // an exception is thrown because the name cannot be converted to long phone. optLong (0); // The default phone value built in the code. optLong (0, 1000); // The default value person provided by the user. optLong ("name"); person. optLong ("name", 1000); // returns the 1000} catch (JSONException ex) {// Exception Handling Code} instead of throwing an exception as above}
In addition to the above two classes, you can also use JSONStringer to build json text
Try {JSONStringer jsonText = new JSONStringer (); // The object starts. Objects and endObject must be paired with jsonText. object (); jsonText. key ("phone"); // the value of the key phone is an array. The array and endArray must be paired with jsonText. array (); jsonText. value ("12345678 "). value ("87654321"); jsonText. endArray (); jsonText. key ("name"); jsonText. value ("yuanzhifei89"); jsonText. key ("age"); jsonText. value (100); jsonText. key ("address"); // The key address value is jsonText. object (); jsonText. key ("country"); jsonText. value ("china"); jsonText. key ("province"); jsonText. value ("jiangsu"); jsonText. endObject (); jsonText. key ("married"); jsonText. value (false); //}. The object ends with jsonText. endObject ();} catch (JSONException ex) {throw new RuntimeException (ex );}
Json text parsing class JSONTokener
Parse json text into corresponding objects according to RFC4627 specifications.
For parsing json text as an object, you only need to use two APIs of this class:
Constructor
Public Object nextValue ();
// {// "Phone": ["12345678", "87654321"], // array // "name": "yuanzhifei89 ", // string // "age": 100, // value // "address": {"country": "china", "province": "jiangsu "}, // object // "married": false // Boolean //} private static final String JSON = "{" + "\" phone \": [\ "12345678 \", \ "87654321 \"], "+" \ "name \": \ "yuanzhifei89 \", "+" \ "age \": 100, "+" \ "address \": {\ "country \": \ "china \", \ "province \" : \ "Jiangsu \"}, "+" \ "married \": false, "+"} "; try {JSONTokener jsonParser = new JSONTokener (JSON ); // at this time, no json text has been read. Direct Reading is a JSONObject object. // If the read location is "name": Now, nextValue is "yuanzhifei89" (String) JSONObject person = (JSONObject) jsonParser. nextValue (); // The next step is to operate person on the JSON object. getJSONArray ("phone"); person. getString ("name"); person. getInt ("age"); person. getJSONObject ("address"); person. getBoolean ("married");} catch (JSONException ex) {// Exception Handling Code}
Other APIs are used to view the text in json text.
Try {JSONTokener jsonParser = new JSONTokener (JSON); // continue to read the characters in 8 json texts. At the beginning, jsonParser. next (8); // {"phone. Tab is a character. // continue to read the characters jsonParser. next (); // "// continue to read the characters in a json text. This character is not a blank character, nor is it the jsonParser character in the eyes. nextClean (); //: // returns the string (excluding a) between the current read location and the first encounter 'A ). JsonParser. nextString ('A'); // ["12345678", "87654321"], "n (there are two spaces in front) // return the string between the current reading position and any character in the first encountered string (such as "0089"). The character is also trimmed. (This is the first time I met 89) jsonParser. nextoff( "0089"); // me ":" yuanzhifei // detaches A jsonParser from the read location. back (); jsonParser. next (); // I // read position forward to the specified string (including string) jsonParser. skipPast ("address"); jsonParser. next (8); // ": {" c // read position forward to the execution character (excluding characters) jsonParser. skipTo ('M'); jsonParser. next (8); // married "} catch (JSONException ex) {// Exception Handling Code}
Note:
// If a json object member is null, either of the following conditions may occur: // 1: The member name is not displayed (corresponding to null in java) // 2: The member value is null. (Corresponding to JSONObject in java. NULL) // complete Json // {// "phone": ["12345678", "87654321"], // array // "name": "yuanzhifei89 ", // string // "age": 100, // value // "address": {"country": "china", "province": "jiangsu "}, // object // "married": false // Boolean // first case: No member name (address) appears) string jsonText = "{" + "\" phone \ ": [\" 12345678 \ ", \" 87654321 \ "]," + "\" name \": \ "yuanzhifei89 \", "+" \ "age \": 100, "+" \ "married \": false, "+ "}"; try {JSONTokener t = new JSONTokener (jsonText); JSONObject obj = (JSONObject) t. nextValue (); if (obj. optJSONObject ("address") = null | obj. isNull ("address") {}} catch (JSONException ex) {ex. printStackTrace ();} // Case 2: The member value is null (address is null) String jsonText = "{" + "\" phone \": [\ "12345678 \", \ "87654321 \"], "+" \ "name \": \ "yuanzhifei89 \", "+" \ "age \": 100, "+" \ "address \": null, "+" \ "married \": false, "+"} "; try {JSONTokener t = new JSONTokener (jsonText ); JSONObject obj = (JSONObject) t. nextValue (); // determine whether json is null if (obj. get ("address") = JSONObject. NULL | obj. isNull ("address") {}} catch (JSONException ex) {ex. printStackTrace ();}
The object in json does not have a correspondence with the object in java.
Null in json corresponds to JSONObject in java. NULL, so jsonObj. put ("key", JSONObject. NULL) is equivalent to {"key": null}. json does not contain a member's corresponding null in Java. Therefore, jsonObj. put ("key", null) is equivalent to deleting this Member, that is :{}