Common backend processing method json

First, what is json?

json javascript originally in the content, may transmit to the rear end to accommodate a variety of data formats front end, it is necessary to convert json use, it is represented by a variety of complex data, such as object, an array, a set of , and a set of other data set.

json is a front end and a rear end of the transmission network lightweight data exchange format , the string is a string, but will use a specific element label symbol. Bis {} brackets indicate the object, [] represents an array in brackets, "" double quotation marks or attribute value: represents the colon which is the former value (this value may be a string, a number, or it may be another array Object).

The rear end of a string can then be converted to the specific string passed through the distal json.

Second, why should json?

Easy to parse this language, a simple client-side JavaScript can parse JSON data through eval (), to get the data through the array and access the object properties.

Third, some common json format

1.JSON objects --JSONObject

var json={"name":"张三" , "age":18 };
var json={"name":"张三", "age"=18,"address":{"street":"湖南", "city":长沙","country":"中国"}};

2, JSON array --JSONArray

var json=[{"name":"张三" , "age":18 },{"name":"李四" , "age":20 }];
var json=[
{"name":"张三", "age"=18,  "address":{"street":"湖南", "city":"长沙","country":"中国"}},
{"name":"李四", "age"=20,  "address":{"street":"湖南", "city":"张家界","country":"中国"}}
]

3、

var JSON = { "ABC": [{ "name": "the txt1"}, { "name", "txt2"}]}; // {} which is an object
Fourth, the conventional back-end processing method json

fastjson is a very good performance of the Java language implementation of JSON parser and generator, engineers have developed from Alibaba.

JSON.parseObject (String str) is converted to the corresponding str objects JSONObject

JSON.parseObject (String str) differs JSONObject.parseObject (String str) is : JSON is an abstract class, there is a static method in JSON parseObject (String text), the text is parsed object and returns a JSONObject; is a succession JSONObject since JSON class, when calling JSONObject.parseObject (result), it will direct calls parseObject parent class (String text). So no difference between the two, is a parent to call the parent class's own static parseObject (String text), is a subclass of the static parseObject to call the parent class (String text), both adjusted to the same method .

// json string into an object 
jsonString = "{\" result \ ": \" success \ ", \" message \ ": \" Success! \ "}"; // or = JSONString '{' idcard ':' 42,313,123 ',' ID ':' 2345 '} "; 
jsonObject = JSONObject.parseObject (JSONString);   
the JSONArray JSONArray = JSONArray.parseArray (jsonObject.getString ( "Data" )); 
 // json string into Map 
the Map <string, string> Map = JSONObject.parseObject (JSONString, the Map. class );
 // the jSONObject convert json string 
string jsonstr = JSON.toJSONString (jsonObject) ;
 // convert String into JSONArray, first into JSONObject, then [] section into the JSONArray 
String str1 = "{\"//data的value可以转为Array 
JSONObject json = JSONObject.parseObject(str1);  
JSONArray jsonArray = JSONArray.parseArray(json.getString("data"));

 

Guess you like

Origin www.cnblogs.com/lcx20190724xxz/p/11237191.html