servlet generates json data returned to Ajax

A, JSON

JSON is a substituted XML data structure, and compared to xml, but it is more compact and does not describe differential ability, because of its small so reducing the network traffic so that more traffic speed.

JSON is just a bunch of string elements will use specific signs while.

Bis {} brackets indicate the object

[] Represents an array in brackets

"" Is in quotation marks or attribute value

: Colon indicates a value which is the former (key: value)

 II. Json data generated, traditional values ​​distal

 

Three .JSONObject, JSONArray difference

 

 json, is a key corresponding to a value, super simple one to one relationship. Now used json that can be nested layers ah, beginning contact, really kind of collapse catch the foot, do not want to reason, but also so much trouble to take data. In fact, just like if else statement, if if if the sets, if re-set if, write a specification okay, if the code format is not standardized, then we must also look at the trouble. So, ah, for nested json, just remember the symbol ":" before the key is, the value of the symbol is to find pairs of braces, a layer of peel clear. For examples, as follows:

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

  To see from outside to inside, the example is an array, which array is two json string format. In this way it is much clearer analysis of ideas.

     Work need to remove the value of name4, how would you take it? . Initially I thought string interception, did not yet understand JSONArray, now know, take it out quite easily.

     Remove name4 values ​​of the process steps of: 1, the string into the above objects JSONArray; 2, first remove the object, the object JSONObject; 3, takes a value of JSONObject objects name1; 4, takes a value of name2 JSONObject objects; 5, taken the value value2 name4.

    Json example string array format can be converted directly by a method JSONArray format:

GetJsonArray = JSONArray.fromObject JSONArray (arrayStr); // convert the result into a form object JSONArray   
the JSONObject getJsonObj = getJsonArray.getJSONObject (0); // Get the first entry in the array json   
String result = getJsonObj.getJSONObject ( "name1" ) .getJSONObject ( "name2") getJSONObject ( "name4").;

 

Well, we talk about these two objects.

1,JSONObject

  json object is a value corresponding to a key, using braces {} such as: {key: value}

2,JSONArray

  json array, use square brackets [], but inside the array of items is key to json format

  Json objects are added key-value pairs, JSONArray Json objects are added

Json = JSONObject new new JSONObject ();   
the JSONArray JsonArray = new new the JSONArray ();   
  
Json.put ( "Key", "value"); // add the key subject of JSONObject   
JsonArray.add (Json); // The objects JSONObject Add to Json array  

Partly from https://blog.csdn.net/qq_32253371/article/details/78083391

Guess you like

Origin www.cnblogs.com/sengzhao666/p/12061157.html