Android Learning Series (--APP) parsing JSON for data format

Source: Internet
Author: User

JSON data format, in Android is widely used in the client and network (or server) communication, it is necessary to learn the system.
I recently made a simple study of JSON, and I would like to summarize it to treat you.
For concise and clear articles, as many as possible, less nonsense.
Reference Document: http://www.ietf.org/rfc/rfc4627.txt?number=4627

1.JSON parsing
(1). Parse one of the object:

1 {"url":"http://www.cnblogs.com/qianxudetianxia"}

Parsing method:

12 JSONObject demoJson = newJSONObject(jsonString);String url = demoJson.getString("url");

  (2). Parse Object Two:

1 {"name":"android","name":"iphone"}

Parsing method:

1234 JSONObject demoJson = newJSONObject(jsonString);String name = demoJson.getString("name");String version = demoJson.getString("version");System.out.println("name:"+name+",version:"+version);

(3). Parse one of the array:

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

Parsing method:

123456 < Code class= "JavaScript plain" >jsonobject Demojson =  new  < Code class= "JavaScript plain" >jsonobject (jsonstring); jsonarray numberlist = Demojson.getjsonarray ( "number" for //because the type in the array is int, so getint, other getstring, Getlong with &NBSP;&NBSP;&NBSP;&NBSP; }

  (4). Parse the second of array:

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

Parsing method:

1234567 //nested array traversal jsonobject Demojson =  new  jsonobject (jsonstring); jsonarray numberlist = Demojson.getjsonarray ( for ( int  i= 0 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; // Gets the array in the array &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; System.out.println (Numberlist.getjsonarray (i). GetInt ( 0 }

  (5). Parse Object and array:

1 {"mobile":[{"name":"android"},{"name":"iphone"}]}

Parsing method:

12345 jsonobject Demojson =  new   jsonobject (jsonstring); jsonarray numberlist = Demojson.getjsonarray ( for ( int  i= 0 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;

  So, we find that getting back is followed by the type of results you want: GetType, which is helpful for understanding.
(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.

1234 //无url节点,抛出异常String url = demoJson.getString("url");//无url节点,返回空,如果为基本类型,则返回默认值String url = demoJson.optString("url");

  (7). UTF-8 BOM Header causes problems parsing JSON exceptions
When the JSON file is saved as Utf-8, the BOM header "ef BB ef" bytes are generated at the front of the text (required to be opened with 16 binary tools) under the Windows platform.
There are two ways to resolve this:
A. Use 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, try several times on it.
B. Use code processing to intercept the contents of the JSON body:

12 String jsonString = getJsonString();jsonString = jsonString.substring(jsonString.indexOf("{"),jsonString.lastIndexOf("}")+1);

2.JSON must know
(1). JSON is a lightweight format for data interchange
(2). JSON is based on two types of data structures: Object and array. Where object is a collection of "name/value" pairs.
(3). Object: Curly braces, each group of String-value are combined with "," delimited, string and value separated by colons.

(4). Array:

(5). String A collection of any number of Unicode characters surrounded by double quotation marks, escaped with a backslash.

(6). Value can be a string enclosed in double quotation marks (string), a numeric value (number), A,,, true object, false null or array. These structures can be nested.

(7). Blanks can be added to any symbol, including spaces, tabs, carriage returns, line breaks, and so on.
(8). For example:
A.object Instances:

12345678910111213 {     "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, 38793]    }}

B.array Instances:

12345678910111213141516171819202122 [   {      "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"   }]

3. Summary
Very simple, very basic, water can become a river, tired bricks can build building.

Android Learning Series (--APP) parsing JSON for data format

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.