JSON-JSON composition and parsing

Source: Internet
Author: User
Tags xml parser

JSON has been used for a long time. A small software has been developed recently and a third-party API needs to be called. Google Translate API uses JSON data format. Douban API uses XML data format. In the process, I can't help but feel that XML is really not suitable for storing data, and it takes a lot of resources to resolve it. Now more and more APIs are using JSON as the data carrier. Facebook's latest Graph API also uses JSON as the data carrier.

JSON is much simpler than JSON. For more information about JSON, see the official documentation http://www.json.org/json-zh.html. JSON is so simple: 1) so that only two classes are required for parsing. 2) even the JSON parsing package is not published in the form of a jar package, but directlySource code(3) Parse only Java-based class libraries, without third-party class libraries. Any of these three points is enough to tear up any XML Parser ......

Before using JSON, download the support package (SourceCode, Just copy the package to the project ). Only jsonobject and jsonarray are classes related to JSON parsing. The remaining classes seem to help produce JSON. JSON may also take into account the user needs to modify the content of these classes, so it is not packaged into jar for release.

The JSON format can be roughly understood through the documentation provided at the beginning. JSON logs are very expressive. JSON is such a format that can be infinitely nested. Below is a simpleProgramIn the field, use the following JSON.

The JSON to be parsed in the program is:

{

"Strvalue": "Here is str value ",

"Nullvalue": NULL, "intvalue": 999,

"Doublevalue": 999,

"Booleanvalue": True,

& Quot; array & quot;: [& quot; A1 & quot;, & quot; a2 & quot;, true, 2, 33.3, null,

{"Innerstr": "Here is a inner Str", "innerinteger": 123456789 },

["Hi, found me? "],

"Innerobj": {"innerstr": "Here is a inner Str", "innerinteger": 123456789}

}

The JSON section covers as many formats as possible in JSON, including key-value pairs, arrays, arrays in the array, jsonobject, Boolean, double, and integer values in the array. The parsing code is as follows:

Package Org. JSON. learn; import Org. JSON. jsonexception; import Org. JSON. jsonobject; public class jsonobjectuseapp {/*** @ Param ARGs * @ throws jsonexception */public static void main (string [] ARGs) throws jsonexception {string jsonstr = "{\" strvalue \ ": \" here is str value \ "," + "\" nullvalue \ ": NULL, "+" \ "intvalue \": 999, "+" \ "doublevalue \": 999, "+" \ "booleanvalue \": True, "+" \ "array \": [\ "A1 \", \ "A2 \", true, 2, 33.3, null, "+" {\ "innerstr \": \ "here is a inner STR \", \ "innerinteger \": 123456789 }, "+" [\ "Hi, found me? \ "]" + "]," + "\" Innerobj \ ": {\" innerstr \ ": \" here is a inner STR \ ", \" innerinteger \": 123456789} "; log (jsonstr); jsonobject JSON = new jsonobject (jsonstr); log (" string from JSON \ t "+ JSON. getstring ("strvalue"); log ("null from JSON \ t" + JSON. get ("nullvalue"); log ("integer from JSON \ t" + JSON. getint ("intvalue"); log ("double from JSON \ t" + JSON. getdouble ("doublevalue"); log ("Boolean from JSON \ t" + JSON. getboolean ("booleanvalue"); log ("string from JSON array \ t" + JSON. getjsonarray ("array "). getstring (0); log ("string from inner JSON object \ t" + JSON. getjsonobject ("innerobj "). getstring ("innerstr"); log ("int from jsonarray from JSON object \ t" + JSON. getjsonarray ("array "). getjsonobject (6 ). getint ("innerinteger"); log ("int from JSON array \ t" + JSON. getjsonarray ("array "). getjsonarray (7 ). getstring (0);} Private Static void log (Object OBJ) {system. out. println (obj. tostring ());}}

Jsonobject is the content in a pair of braces, which are key-value pairs. When the value is set, the value of the value is obtained based on the key. The key must be a string, enclosed in double quotation marks, followed by a colon, followed by a value. The value can be string, Int, Boolean, integer, or double, or another jsonobject or jsonarray.The value in jsonarray can also be any value, but there is no key, so it can only be based on the index value.Jsonobject and jsonarray can be nested with each other.

I think it is easy to use JSON to represent data. Google's protobuffer is another enhanced JSON type. For JSON, the content still needs to be parsed. protobuffer allows users to define the schema to specify the data format, and then generate the corresponding class after compilation. Class contains data binding, serialization, deserialization, and other operations. It can be said that the data format is completely transparent to users. The format is small and fast.

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.