Jsonobject, Jsonarray Difference

Source: Internet
Author: User

JSON, is a single key corresponding to a value, super simple one-to-one relationship. Now used in the JSON that can be nested ah, just start contact, there is a collapse of the bright, do not want to go to the rationale, take a data is still so troublesome. In fact, just like if Else statement, if the IF in the set if,if 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 the symbol ":" is the key, after the symbol is the value of curly braces in pairs to find, a layer of peel off, it is clear. As an example, the following:

[{name1:{name2:{name3: ' value1 ', name4: ' value2 '}}},{}]

From the outside, the example is an array of two JSON-formatted strings. So the analysis of ideas is much clearer.

What do you do when you need to take out the Name4 value in your work? I thought about string interception at first, and I didn't know Jsonarray, and now I know it's pretty easy to take out.

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

The JSON array-formatted string in the example can be converted directly to the Jsonarray format by means of the method:

Jsonarray.fromobject (String)

    1. Jsonarray Getjsonarray=jsonarray.fromobject (ARRAYSTR); //Convert the result to the form of a Jsonarray object
    2. Jsonobject getjsonobj = Getjsonarray.getjsonobject (0); Gets the first item in the JSON array
    3. String result=getjsonobj.getjsonobject ("name1"). Getjsonobject ("name2"). Getjsonobject ("name4");

All right, let's talk about these two objects.

1,jsonobject

JSON object, which is a key corresponding to a value, using curly 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

    1. Jsonobject Json = new Jsonobject ();
    2. Jsonarray Jsonarray = new Jsonarray ();
    3. Json.put ("key", "value"); Adding a key-value pair to the Jsonobject object
    4. Jsonarray.add (Json); //Add the Jsonobject object to the JSON array

3,jsonobject and map

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

    1. map<string,string> strmap=New Jsonobject ();

The requirement here is that the map retrieved by the request object will not be laborious to return to the JSON format.

Jsonobject, Jsonarray Difference

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.