Android Data Format Parsing object JSON usage

Source: Internet
Author: User


1. JSON concept:

A lightweight data exchange format with good readability and ease of writing, allowing data exchange between different platforms. JSON adopts a highly compatible text format and has behaviors similar to the C language system.

JSON can convert a Java object to a JSON string and convert a JSON string to Java. It is more lightweight than XML, and JSON is relatively lightweight and simple to use. JSON data format is widely used in Android for communication between clients and servers. It is very convenient for network data transmission and resolution.


2. Environment Configuration

In fact, in the 3.0 platform, this part is directly integrated into Android. We want to parse JSON data, download a jar package directly on the website, and import it to the project to parse JSON data.

3. JSON Application

(1). JSON is a lightweight data exchange format.
(2) JSON is based on two data structures: object and array. The object is a set of "name/value" pairs.

(3) Android contains four JSON-related classes and one exceptions:

A. jsonobject

This is the basic unit related to the json definition in the system. It contains a pair of key/value values.

B. jsonarray

It represents a group of ordered values. The format of tostring output is enclosed in square brackets. Values are separated by commas (,) ([value1, value2, value3]).

C. jsonstringer

This class can help you quickly and conveniently create jsontext. Its biggest advantage is that it can reduce the number of errors caused by format errors.ProgramException. You can reference this class to automatically create JSON text in strict accordance with the JSON syntax rules (syntaxrules. Each jsonstringer object can only create one JSON text. For example:

String mystring =NewJsonstringer (). Object ()
. Key ("name ")
. Value ("Piggy ")
. Endobject ()
. Tostring ();

Mystring = {"name": ""}

D. jsontokener

This is the class in which the system parses the JSON source string for the jsonobject and jsonarray constructors. It can extract numerical information from the source string.

E. jsonexception


(4) Example of JSON format

A. ObjectInstance:


"Image ":{
"Width": 800,
"Height": 600,
"Title": "view from 15th floor ",
"Thumbnail ":{
"Url": " ",
"Height": 125,
"Width": "100"
"IDS": [116,943,234,387 93]

B. ArrayInstance:


"Precision": "Zip ",
"Latitude": 37.7668,
"Address ":"",
"City": "San Francisco ",
"State": "ca ",
"Zip": "94107 ",
"Country": "us"

"Precision": "Zip ",
"Latitude": 37.371991,
"Address ":"",
"City": "Sunnyvale ",
"State": "ca ",
"Zip": "94085 ",
"Country": "us"

4. JSON parsing case
(1) parsing one of the objects:

String jsonstring = {"url": " "};

Resolution method:

Jsonobject demojson =NewJsonobject (jsonstring );

String url = demojson. getstring ("url ");

(2) parsing object 2:

String jsonstring = {"name": "android", "version": "beta1.0 "};

Resolution method:

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 arrays:

String jsonstring = {"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:

String jsonstring = {"Number": [[1], [2], [3]};

Resolution method:

//Nested array Traversal
Jsonobject demojson =NewJsonobject (jsonstring );
Jsonarray numberlist = demojson. getjsonarray ("Number ");

For(IntI = 0; I <numberlist. Length (); I ++ ){
//Returns an array.
System. Out. println (numberlist. getjsonarray (I). getint (0 ));

(5 ).AnalysisObjectAndArray:

String jsonstring = {"mobile": [{"name": "android" },{ "name": "iPhone"}]};

Resolution method:

Jsonobject demojson =NewJsonobject (jsonstring );
Jsonarray numberlist = demojson. getjsonarray ("mobile ");

For(IntI = 0; I <numberlist. Length (); I ++ ){
System. Out. println (numberlist. getjsonobject (I). getstring ("name "));

(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. An exception is thrown.
String url = demojson. getstring ("url ");

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

(7). UTF-8OfBOMHeader causes ParsingJSONException
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 );

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: 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.