The basic format of JSON and the conversion to JavaScript
Recently, many people have found that when writing JSON-formatted data, it is not always possible to achieve the JSON specification, although it can be passed without the language requirements being strict.
However, inevitably will encounter such a problem, the time to make changes, worried about the late, so plan to write this blog post.
Syntax rules for JSON:
1. Data needs to be written in the form of key-value pairs;
E G:
{ "name": "Zychao" }
2, data and data need to be separated by commas;
E G:
{ "name": "Zychao", "sex": "Male", "age":
3. The data object needs to be covered by a pair of curly braces;
4, the ordered list of values;
E G: In the following case, the object users is an array, and each object is a user record in an array of key values.
{ "users" : [ {"firstName": "He", "LastName": "Yongqing"}, {"firstName": "Ding", "LastName": "Yingjie"}, {"fitstname": "Lucene", "LastName": "Zhang"}]}
JSON text converted to JavaScript object
We can parse the JSON string literal through the Json.parse () function and convert it to a JavaScript object.
var text = "{' users ': [" + "{' FirstName ': ' He ', ' lastName ': ' Yongqing '}," + "{' FirstName ': ' Ding ', ' lastName ' : ' Yingjie '} "+"]} ";
var obj = json.parse (text);
Finally, we can apply the JavaScript object to the written page.
The basic format of JSON and the conversion to JavaScript