1, json array basic format
1.1 meaning of symbols in JSON data
First show a piece of json code, which is named result and defined as String type
{ "retCode":0, "retMSg":"success", "data":[ { "name":"Li Lei", "id":"001", "score":{ "Chinese":"80", "Math":"95" }, "schoolInfo": [ { "School_name": "tsinghua" }, { "School_name": "Peking University" } ], }, { "name":"Mei Mei Han", "id":"002", "score":{ "Chinese":"90", "Math":"80" }, "schoolInfo": [ { "School_name": "tsinghua" }, { "School_name": "Peking University" } } ] }
json internal format:
1 [] brackets represent an array
2 {} braces represent an object. An object may also contain attributes of the object, or it may be another class
3 the double quotation mark "" indicates the attribute value
4 colon: represents the relationship between before and after. The colon is preceded by the name of the attribute and followed by the value of the attribute. This value can be either a basic data type or a reference data type.
Taking the above code as an example, the first layer is a curly bracket {} json array. The json array contains three attributes: retCode,retMSg and data
The second layer is the JsonArray attribute of data. Each curly bracket represents an object. Each object has four attributes: name, ID, score and schoolinfo.
The third layer contains a class under the score attribute, which contains two attributes: Chinese and Math.
schoolInfo is a JsonArray array containing School_name attribute
1.2 method provided by Jason
To resolve the JSONObject system's own parsing method, let's start with the methods in the JSONObject (system's own class)
The common construction method of JsonObjcet is
JsonObject.paresObject("text")
The most commonly used values are:
JsonObject.getString("key"); JsonObject.getJsonArray("key")
The official methods are as follows:
2, Parsing of Json array
Get and parse each value in the result
Preparation before parsing:
You can convert the result of String type to Json array type
JsonObject jsonObject=JsonObject.parseObject(result);
Basic data type:
String returncode=jsonObject.getString("reusult Properties in");
Reference data type: (array)
JSONArray parseArray=jsonObject.getJsonArray("result Array type in");
3, Analytical process
Analyze the above code:
//Convert to jsonObject type JsonObject jsonObject=JSONObject.parseOject(result); //Get attribute value String returnCode=jsonObject.getString("retCode"); String returnMsg=jsonObject.getString("retMSg"); //Get array value JSONArray parseArray=jsonObject.getJsonArray("data"); //Handle the inside of the array for(int i=0;i<parseArray.size();i++){ //It is the same as the regular array get(i), but it is of JSONObject type //eachObject is each class {} in the array. Because the properties contained in the class are the same, it is equivalent to circular processing JsonObject eachObject=parseArray.getJSONObject(i); //Properties of the second layer String name=eachObject.getString("name"); String id=eachObject.getString("id"); //Handle the JSONObject object sorce of the third layer JSONObject sorceObject=eachObject.getJSONObject("sorce"); //Handle the properties in the third layer JSONObject object sorce String Chinese=sorceObject.getString("Chinese"); String Math=sorceObject.getString("Math"); //Handle the schoolInfo attribute in the third layer JSONObject object JSONArray schoolInfoArray = eachObject.getJSONArray("schoolInfo"); for (int j = 0; j < schoolInfoArray.length(); j++) { JSONObject schoolInfojson = schoolInfoArray.getJSONObject(j); String schoolName = schoolInfojson.getString("School_name"); } } }
Parsing complete
4, JsonObject assignment process
Equivalent to the inverse process of analytical process
4.1 native JsonObject
JsonObject jsonObject=new JsonObject(); JsonArray applist=new JsonArray(); JsonObject obj=JsonObject.parseObject("{ "name":"Li Lei", "id":"001", "score":{ "Chinese":"80", "Math":"95" }, "schoolInfo": [ { "School_name": "tsinghua" }, { "School_name": "Peking University" } ], }, { "name":"Mei Mei Han", "id":"002", "score":{ "Chinese":"90", "Math":"80" }, "schoolInfo": [ { "School_name": "tsinghua" }, { "School_name": "Peking University" } }"); applist.add(obj); jsonObject.put("retCode",0); jsonObject.put("retMSg","success"); jsonObject.put("data",applist); )
4.2 HashMap java bean
Because I haven't practiced it yet, I posted the link directly:
Define a json array and assign values
5, References
If there are mistakes, you are welcome to criticize and correct them.