Android Json parsing and summary

Source: Internet
Author: User




1. JSON-defined JSON (JavaScript Object Notation) is a lightweight data exchange format. Easy to read and write. It is also easy to parse and generate machines. It is based on a subset of JavaScript Programming Language, Standard ECMA-262 3rd Edition-December 1999. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, JavaScript, Perl, Python, and so on ). These features make JSON an ideal data exchange language.

2. JSON format is constructed in two structures:
A collection of name/value pairs ). In different languages, it is understood as an object, record, struct, dictionary, and hash table ), keyed list or associative array ).
An ordered list of values ). In most languages, it is understood as an array ).

JSON has the following forms:

An object is an unordered set of 'name/value' pairs. An object starts with "{" (left parenthesis) and ends with "}" (right Parenthesis. Each "name" is followed by a ":" (colon); "," (comma) is used to separate the "name/value" pairs.

An array is an ordered set of values. An array starts with "[" (left square brackets) and ends with "]" (right square brackets. Values are separated by commas.

Value can be a string, number, true, false, null, object, or array enclosed by double quotation marks ). These structures can be nested. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140321/2014032112540745.gif" width = "598" height = "278" alt = "\">



The above content is taken from the Json official website.

Iii. Common JSON parsing Classes 1. All Android JSON-related classes are under the org. json package JSONObject, JSONArray, JSONException, JSONStringer, JSONTokener
2. What is the difference between the get method and the opt method in common methods?
We recommend that you use the opt method for the JsonObject method (opt * And get *), because if the content of the get method is empty, an exception is thrown directly. However, JsonArray. opt * (index) may be out-of-bounds and requires special attention.
opt、optBoolean、optDouble、optInt、optLong、optString、optJSONArray、optJSONObjectget、getBoolean、getDouble、getInt、getLong、getString、getJSONArray、getJSONObject



3. Create JSON for Android
    private String createJson() throws JSONException {     JSONObject jsonObject = new JSONObject();     jsonObject.put ("intKey" , 123);     jsonObject.put ("doubleKey" , 10.1);     jsonObject.put ("longKey" , 666666666);     jsonObject.put ("stringKey" , "lalala" );     jsonObject.put ("booleanKey" , true);          JSONArray jsonArray = new JSONArray();     jsonArray.put (0, 111);     jsonArray.put("second");     jsonObject.put ("arrayKey" , jsonArray);          JSONObject innerJsonObject = new JSONObject();     innerJsonObject.put ("innerStr" , "inner" );     jsonObject.put ("innerObjectKey" , innerJsonObject);               Log.e("Json" , jsonObject.toString());          return jsonObject.toString();     }

Output result: {"intKey": 123, "doubleKey": 10.1, "longKey": 666666666, "stringKey": "lalala", "booleanKey": true, "arrayKey ": [111, "second"], "innerObjectKey": {"innerStr": "inner "}}



4. parse the JSON file created above
    private void pareJson(String jsonStr) throws JSONException {     JSONObject jsonObject = new JSONObject(jsonStr);     int intValue       = jsonObject.optInt( "intKey");     double doubleValue = jsonObject.optDouble( "doubleKey");     long longValue     = jsonObject.optLong( "longKey");     String strValue    = jsonObject.optString( "stringKey");     boolean boolValue  = jsonObject.optBoolean( "booleanKey");          JSONArray array    = jsonObject.optJSONArray( "arrayKey");     int arrIntValue    = array.optInt(0);     String arrStrValue = array.optString(1);          JSONObject innerJson = jsonObject.optJSONObject("innerObjectKey" );     String innerStr      = innerJson.optString( "innerStr");          Log.e("Json" , "intValue = " + intValue + " , doubleValue = " + doubleValue                + " , longValue = " + longValue + " , strValue = " + strValue                + " , booleanValue = " + boolValue + " , arrIntValue = " + arrIntValue                + " , arrStrValue = " + arrStrValue + " , innerStr = " + innerStr);    }

Output result: intValue = 123, doubleValue = 10.1, longValue = 666666666, strValue = lalala, booleanValue = true, arrIntValue = 111, arrStrValue = second, innerStr = inner


For more information, see:
Android json parsing and simple example
Android study notes 44: JSON data parsing

4. the Android JSON parser library uses the native class parsing JSON provided by Android. The biggest advantage is that the project does not need to introduce a third-party library, however, if you focus on development efficiency and do not care about the application size increasing by several hundred kb, you can choose from the following JSON:
1. Jackson
2. google-gson
3. Json-lib

Performance Comparison Between Two JSON class libraries Jackson and JSON-lib (third test added)

5. During daily development of the formatting tool, if debugging protocols with the server are involved, problems may occur in the sending format of the server or the parsing format of the Android client, in this case, you need to print the obtained JSON to locate the problem. If the JSON is short, you can see it at a glance, however, if you want to search for a field or a value in a JSON array for a long time, it is very difficult to get rid of this annoyance. After formatting the JSON string, you will find that the distress is instantly invisible. The following is an example of my commonly used Notepad ++. Other Editors certainly have corresponding JSON plug-ins. You can find them online.
Notpad ++ Json Viewer plug-in
Installation:
Notpad ++-> ins-> Plugin Manager-> Show Plugin Manager-> Avaliable-> select Json View-> install)
Usage:
Select a Json string,
Plug-in (Plugins)-> Format Json (shortcut: Ctrl + Alt + Shift + M)
Ins-> Show Json Viewer display Json View




Related Article

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.