Android Learning Series (20)-parsing JSON of APP data format

Source: Internet
Author: User

The JSON data format is widely used in Android for client-to-Network (or server) communication. It is very necessary for you to learn about it systematically.
I have recently made a simple study on json. I would like to summarize it to everyone.
ToArticleConcise and clear, with as many columns as possible and less nonsense.
Reference: http://www.ietf.org/rfc/rfc4627.txt? Number = 4627

1. JSON Parsing
(1). parse one of the objects:

 
{"Url": "http://www.cnblogs.com/qianxudetianxia "}

Resolution method:

 
Jsonobject demojson = new jsonobject (jsonstring); string url = demojson. getstring ("url ");

(2). parse object 2:

 
{"Name": "android", "name": "iPhone "}

Resolution method:

Jsonobject demojson = new jsonobject (jsonstring); string name = demojson. getstring ("name"); string version = demojson. getstring ("version"); system. out. println ("name:" + name + ", version:" + version );

(3). parse one of the arrays:

 
{"Number": [1, 2, 3]}

Resolution method:

 
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, It is getint, and other getstring and getlong are the same as system. out. println (numberlist. getint (I ));}

(4). parse array 2:

 
{"Number": [[1], [2], [3]}

Resolution method:

// Nested array traversal jsonobject demojson = new jsonobject (jsonstring); jsonarray numberlist = demojson. getjsonarray ("Number"); For (INT I = 0; I <numberlist. length (); I ++) {// obtain the array system. out. println (numberlist. getjsonarray (I ). getint (0 ));}

(5). parse object and array:

 
{"Mobile": [{"name": "android" },{ "name": "iPhone"}]}

Resolution method:

 
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 "));}

Therefore, we find that get is followed by the type of the expected result: GetType, which is helpful for understanding.
(6). Use opttype:
In the preceding example, when GetType is used, an exception is thrown when the node cannot be found.
If opttype is used and no node is found, null or default value is returned.

 
// No URL node, throwing an exception string url = demojson. getstring ("url"); // No URL node. null is returned. If it is of the basic type, the default value string url = demojson is returned. optstring ("url ");

(7). UTF-8 BOM header causes JSON parsing exceptions
When the JSON file is saved as UTF-8, the BOM Header "Ef bb ef" is generated at the beginning of the text (which can be viewed only when it is opened using a hexadecimal tool ).
There are two solutions:
A. Use ultraedit to open the JSON file, save as, select the format of UTF-8, no Bom header, if not, open in notepad, save as UTF-8, try multiple times on it.
B. UseCodeProcess and intercept the JSON body content:

String jsonstring = getjsonstring (); jsonstring = jsonstring. substring (jsonstring. indexof ("{"), jsonstring. lastindexof ("}") + 1 );

2. JSON is required
(1). JSON Is a lightweight data exchange format
(2) JSON is based on two data structures: object and array. Where object is The set of "name/value" pairs.
(3). Object: braces. Each string-value group is separated by ",", and string and value are separated by colons.
 
(4). array:

(5). String A set of any number of Unicode characters enclosed by double quotation marks. escape by backslash.

(6). Value It can be a string enclosed by double quotation marks ( String ), Value (number ), True , False ,   Null Object or array ). These structures can be nested.

(7 ). Blank spaces can be added to any symbol, including spaces, tabs, carriage return, and line breaks.
(8). Example:
A. object instance:

 
{"Image": {"width": 800, "height": 600, "title": "view from 15th floor", "thumbnail": {"url ": "http://www.example.com/image/481989943", "height": 125, "width": "100"}, "IDS": [116,943,234,387 93]}

B. array instance:

[{"Precision": "Zip", "latitude": 37.7668, "longpolling":-122.3959, "Address": "", "city": "San Francisco ", "State": "ca", "Zip": "94107", "country": "us" },{ "precision": "Zip", "latitude": 37.371991, "longpolling":-122.026020, "Address": "", "city": "Sunnyvale", "State": "ca", "Zip": "94085 ", "country": "us"}]

3. Summary
It is very simple. It is very basic. Only when the water accumulation reaches the river and the bricks are used to build a building.

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.