Jsonobject, Jsonarray format detailed

Source: Internet
Author: User

JSON and map have been the most exposed in the last two weeks.

The JSON used before, is a key corresponding to a value, super simple one-to-one relationship. Now use the JSON that can be nested, ah, when the first contact, there is indeed a collapse of the hurrying feet, do not want to go to, take a data is so troublesome. In fact, just like if Else statement, if the set if,if in the IF, write the specification is OK, if the code format is not standardized, then we must also look at the trouble. So, for JSON nesting, just remember that the symbol ":" is the key, after the symbol is the value of braces into pairs, a layer of peel off, it is clear. As an example, the following:


From the outside, the example is an array of two JSON-formatted strings in the array. This analysis of the idea is much clearer.

The work needs to take out the Name4 value, how will you take it? At first I thought about the string interception, then did not understand Jsonarray, now know, take out is quite easy.


Remove Name4 Value Process step: 1, convert the above string to Jsonarray object, 2, take out the first item of object, Jsonobject object, 3, remove name1 Value Jsonobject object, 4, remove name2 value Jsonobject object; 5, Remove the Name4 value value2.

The string in the JSON array format in the example can be directly converted to Jsonarray format by means of a method: Jsonarray.fromobject (String)

[Java] View plain copy print? </pre><pre name= "code" class= "Java" >jsonarray getjsonarray=jsonarray.fromobject (ARRAYSTR); Converts the result to the form of a Jsonarray object jsonobject getjsonobj = getjsonarray.getjsonobject (0);//Gets the first String in the JSON array result= Getjsonobj.getjsonobject ("Name1"). Getjsonobject ("Name2"). Getjsonobject ("Name4");

Okay, let's talk about these two objects.

1,jsonobject

The JSON object is a key that corresponds to a value that uses braces {}, such as: {Key:value}

2,jsonarray

JSON array, using brackets [], except that the items inside the array are also JSON key-value pairs


The JSON object adds a key-value pair, and the JSON object is added to the Jsonarray

[Java] View plain copy print?   Jsonobject Json = new Jsonobject ();      Jsonarray Jsonarray = new Jsonarray (); Json.put ("Key", "value"); Add the Jsonobject object to the JSON array by adding the key value pair Jsonarray.add (JSON) to the//jsonobject object

3,jsonobject and map

Both the map map and JSON are key-value pairs, and the difference is that the key values in the map are separated by an equal sign, and the key values in the JSON are separated by colons in the middle. In fact, JSON is a special form of map.

[Java] View plain copy print? Map<string,string> strmap=new jsonobject ();

The requirement here is that the request object gets the map, and it doesn't have to be a bother to return to the JSON format .... Ha ha..


Now I can say that I know map,json very well, when I was learning that, I could say that I had a headache when I mentioned map, and I had trouble with the simple application of map and JSON. Now I work and pick up and learn again. Come out and mix, sooner or later the problem is yours, haha ...


From: http://blog.csdn.net/lishuangzhe7047/article/details/28880009   Thank you for the beautiful and beautiful female programmer

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.