JSONObject、JSONArray

tags: Java development foundation  json

The JSON used before, is a key corresponding to a value, a super simple one-to-one relationship. Now useful JSON that can be nestled in layers, taking a data so cumbersome. In fact, just like the IF else statement, if the IF, IF, IF is set, and write the specification is ok, if the code format is not standardized, then we must also look at it. Therefore, for JSON nesting, just remember the symbol ":", it is the key to the rigidity after the symbol, and the layer is stripped, it will be clear. For example, as follows:

From the outside to the outside, the example is an array, and the array is a string of two JSON formats. This analysis is more clear.
How do you take it out of the value of Name4 in your work? . Initially I wanted to take a string intercepted, I still don't know JSonarray. Now I know, it is quite easy to take it out.

Remove the Name4 value process step: 1, convert the above string to the JSonarray object; 2. Remove the first item of the object, the JSONObject object; Name4 value value2.

The string of the JSON array format can be directly converted to Jsonarray in the format of JSonarray through methods: jsonarray.fromObject (string)

</pre><pre name="code" class="java">JSONArray getJsonArray=JSONArray.fromObject(arrayStr);// Convert the result into the form of a JSonarray object
JSONObject getJsonObj = getJsonArray.getJSONObject(0);/ / Get the first item in the JSON array
String result=getJsonObj.getJSONObject("name1").getJSONObject("name2").getJSONObject("name4");

Well, we talk about these two objects.
1.JSONObject
The JSON object is a key corresponding to a value, using a large bracket {}, such as: {key: value}

2.JSONArray
JSON array, using parentheses [], only the items inside the array are also the JSON key value pair format

The JSON object is added to the key value pair, and the JSONA object is added to JSONARRAY.

JSONObject Json = new JSONObject();
JSONArray JsonArray = new JSONArray();
 
Json.put("key", "value");// JSONObject object Add key value pair
JsonArray.add(Json);// Add JSONOBJECT object to the JSON array

3.jsonObject and MAP
Map and json are all key values, and the MAP is different from the MAP, and the MAP is separated. The JSON key value is separated from the colon in the middle. In fact, JSON is a special form of MAP.

Map<String,String> strmap=new JSONObject();

The demand here is that the MAP acquired by the Request object, and you don't have to return JSON format.

Summary: China brackets are arrays, braces are objects.

Intelligent Recommendation

Add objects to JsonObject and JsonArray

Student class Test case Console: output...

Android JsonObject and JsonArray parsing

Example 1: JSON file program: Example two: program: JSON file Example three program:  ...

Fastjson's JSONArray and JSONObject

[Image upload failed... (image-6adce8-1522634020763)] What is JSON? JSON (JavaScript Object Notation) is a lightweight data exchange format. Easy for people to read and write. It is also easy to machi...

More Recommendation

JsonArray and JsonObject traversal methods

One: traverse JsonArray Two: traversing JsonObject...

List to JsonArray and JsonObject

Reference URL:  ...

JSONObject and JSONArray in json-lib

 Json-lib is used to complete the transformation of beans, collections, maps, java arrays, XML and JSON between jars, json-lib depends on several other jar packages (I tried all jars with the latest v...

springb2.0.4 in JSONObject and use JSONArray

First introduced jar package: JSONObject Usage: you may receive data format: { "id": "123", "courseID": "huangt-test", "title": "Submit Job"...

JSONObject and indiscriminate use of JSONArray

Recently, the company developed several projects in the background Action are Json format to the front-end data transfer, so to JSONObject, JSONArray simply study a little, ado, want to use JSONObject...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top