Constructing and parsing JSON in Java

Source: Internet
Author: User

What is Json?

JSON (Jvascript Object Notation) (official website: http://www.json.org/) is a lightweight data interchange format.
Easy for people to read and write. It is also easy for machine parsing and generation. It is based on JavaScript programming Language, Standard ECMA-262 a subset of 3rd Edition-december 1999.
JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, C #, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language.

Two kinds of structure of JSON
 1. Collection of "name/value" pairs (A collection of name/value pairs)
In different languages, it is understood as objects (object), record (record), structure (struct), Dictionary (dictionary), hash table (hash table), keyed list (keyed list), or associative array (associative Array).
In the Java language, we can interpret it as HashMap.
An object is an unordered collection of "name/value pairs". An object starts with "{" (opening parenthesis) and "}" (the closing parenthesis) ends. Each "name" is followed by a ":" (colon); "' Name/value ' pair ' is separated by", "(comma).
Example: var json = {"name": "Jack", "age": All, "Marray": true};

  

 2. Ordered list of values (an ordered list of values)
In most languages, it is understood as an array (array or List).
An array is an ordered collection of values (value). An array begins with "[" (the left square bracket), and "]" (the right square bracket) ends. Use "," (comma) to separate values.
Example: var json = ["Jack", "Rose", "Tom", 89,true,false];

How JSON is manipulated in Java: Org.json.jar

/*** Parse JSON*/PublicStaticvoidParsingjson () {String json = "{\" resultcode\ ": \" 200\ ", \" reason\ ": \" successful return \ ", \" result\ ": {\" gd\ ": {\" province\ ": \" Guangdong \ " , \ "province_code\": \ "gd\", \ "Citys\": [{"+" \ "city_name\": \ "Meizhou \", \ "city_code\": \ "gd_meizhou\", \ "abbr\": \ "Yue \"}, "+" {\ "city_name\": \ "Shanwei \", \ "city_code\": \ "gd_shanwei\", \ "abbr\": \ "Yue \"}]}},\ "Error_code\": 0} ";Try{Jsonobject Jsonobject =NewJsonobject (JSON); String ResultCode = jsonobject.getstring ("ResultCode"); Jsonobject Jsonone = jsonobject.getjsonobject ("result"); Jsonobject jsontwo = Jsonone.getjsonobject ("GD"); Jsonarray Jsonarray = Jsontwo.getjsonarray ("Citys"); For (int i = 0; i < jsonarray.length (); i++) {jsonobject ject = jsonarray.getjsonobject (i); String S1 = ject.getstring ("City_name"); String s2 = ject.getstring ("City_code"); String s3 = ject.getstring ("abbr");}} Catch (Exception e) { // TODO auto-generated catch block e.printstacktrace ();}}  
/*** Construct JSON*/PublicStaticvoidConstructingjson () {Jsonobject Jsonfirst =NewJsonobject (); Jsonobject Jsonsecond =NewJsonobject (); Jsonobject Jsonthird =NewJsonobject ();Try{Jsonarray Citys =NewJsonarray (); Jsonobject Cityone =NewJsonobject (); Jsonobject Citytwo =NewJsonobject (); Cityone.put ("City_name", "Meizhou"catch (Exception e) {e.printstacktrace ();} System.out.println (Jsonfirst.tostring ()); }

Constructing and parsing JSON in Java

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.