1. JSON data structure (object and array)
JSON object: var obj = {"Name": "Xiao", "Age": 12};
JSON array: var objarray = [{' Name ': ' Xiao ', ' age ': 12},{' name ': ' Xiao ', ' Age ': 12}];
2, processing JSON data, dependent files are: jquery.js
3. Note: In the process of data transmission, JSON data is in the form of text, that is, string format;
JS language is the operation of JS objects;
So the conversion between JSON string and JS object is the key;
4. Data format
JSON string: var json_str = ' {' name ': ' Xiao ', ' Age ': 12} ';
Josn object: var obj = {"Name": "Xiao", "Age": 12};
JS Object: Object = {name: "Xiao", age:12}
5. Type conversion
JSON string-->js object, using the method:
Indicate:
Json_str and obj represent the data types in sub Heading 4 of this article;
obj = Json.parse (JSON_STR);
obj = Jquery.parsejson (JSON_STR);
Note: Incoming malformed JSON string (for example: ' {name: ' Xiao ', age:12} ') throws an exception;
JSON string format, strict format: ' {' name ': ' Xiao ', ' Age ': 12} '
JS Object-->json String:
Json_str = JSON. Stringify (obj);
Note:
1, eval () is a JS native function, using the form: eval (' + ' {name: "Xiao", Age:12} ') '), is not safe, can not guarantee the conversion of the type to JS object;
2, the above 3 methods, have passed the Chrome browser test, the following is a screenshot of the test results;
JSON string-->js object;
JS Object-->json String:
To Garden Friends:
For any errors in the text, you are welcome to point out.
The above discussion of JS JSON data processing is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud-dwelling community.