Analysis of the Usage Methods of JSONObject and JSONArray
- 2021-08-28 19:12:20
- OfStack
Import required: json-lib-2. 2.2-. jar package
1. json: It is a super simple 1-to-1 relationship with one key corresponding to one value. For json nesting, just remember that the symbol ":" is preceded by the key and the symbol is followed by the value braces.
String arrayStr=[{name1:{name2:{name3:'value1',name4:'value2'}}},{}]
Procedures for extracting name4 values:
1) Convert the above string to an JSONArray object; 2) Taking out the first item of the object, the JSONObject object; 3) Taking out the value JSONObject object of name1; 4) Taking out the value JSONObject object of name2; 5) Taking out the value value2 of name4;The string in the json array format in the example can be directly converted to the JSONArray format through the method:
JSONArray.fromObject(String)
JSONArray getJsonArray = JSONArray. fromObject (arrayStr); //Convert the result to an JSONArray object
JSONObject getJsonObj = getJsonArray. getJSONObject (0); //Get the first item in the json array
String result=getJsonObj.getJSONObject("name1").getJSONObject("name2").getJSONObject("name4");
2.JSONObject
json object, that is, one key corresponds to one value, using curly braces {}, such as: {key: value}
3.JSONArray
json array, using brackets [], except that the items in the array are also in json key-value pair format
The key-value pair is added to the Json object, and the Json object is added to the JSONArray object
JSONObject Json = new JSONObject();
JSONArray JsonArray = new JSONArray();
Json. put ("key", "value"); //Add a key-value pair to an JSONObject object
JsonArray. add (Json); //Add an JSONObject object to an Json array
4. Map map and json are both key-value pairs, but the difference is that the key-value pairs in map are separated by an equal sign and the key-value pairs in json are separated by a colon. In fact, json is a special form of map.
Function of JSONObject and JSONArray: Do not refresh jsp page to write back the query results:
/* Example: */
Map<String,String> strmap=new JSONObject();
Map map = new HashMap();
map.put("cudenddate", cudenddate);
map.put("cudbegindate", cudbegindate);
List auditList = kyDataHeadAuditService.getAuditAllDataList(map,paper, currentPage, pageDirection, 10);
map.put("auditList",auditList);
map.put("currentpage", String.valueOf(paper.getCurrentpage()));
map.put("allSize", String.valueOf(paper.getAllSize()));
response.getWriter().write(JSONArray.fromObject(map).toString());
response.getWriter().flush();
response.getWriter().close();
$.post("kyDataHeadAuditAction.do?action=findKyHeadAuditList",{
orgLevel : levs,
sbtype : sbType,
jytype : jyType}
function(data) {
var arr = data[0].auditList;
if (arr.length > 0) {
var html = "";
for ( var i = 0; i < arr.length; i++) {// Update list
var kyReportBean = arr[i];
html += "<tr><td><input type='checkbox' name='kyreportid' value='"
+ kyReportBean.kyReportid
+ "'/> </td>";
html += "<td align='center'>"
+ kyReportBean.kyReportid
+ " </td>";
html += "<td align='center'><input type='button' value=' View ' onclick=\"bmSearchAudit('kyDataAuditAction.do?action=searchAuditDetail&insurCode="+kyReportBean.kyReportid+"')\">" + " </td></tr>";
}
var allsize = data[0].allSize;
var currentpage = data[0].currentpage;
cleartable(0);// Empty a form
$("#tablelist").append(html);
hideLoading();// Cancel animation
$("#allSize").append(data[0].allSize);
$("#currentpage").append((parseInt(data[0].currentpage) + parseInt(1)));
changePage(allsize, currentpage);// Update page turning
$("#currentpagevalue").val(currentpage);
} else
cleartable(0);
hideLoading();// Cancel animation
}, "json");