I just saw it here these days.
JSON(JavaScript Object Notation) is a lightweight data format that uses completely language-independent text formats and is an ideal data interchange format. At the same time, JSON is a native JavaScript format, which means that working with JSON data in JavaScript does not require any special APIs or toolkits and is highly efficient.
Basic lattice :varjsondata= ' {"data1": "Hello,","data2": "world!" } '
Call Method Jsondata.data1,jsondata.data2
Many JSON data are stored in arrays
var jsondata=[{"name": "Lilei", "age": +, "sex": "Male"},{"name": "Hanmei", "Age":, "Sex": "Famale"}]
Call Method Jsondata[0].name,jsondata[1].sex
In general, JSON is relatively easy to understand and use, but at the same time there are a lot of traps, if not pay attention to easily fall in.
the parsing method of JSON
There are two ways to parse JSON: Eval_r () and Json.parse (), using the following methods:
var jsondata = ' {' data1 ': ' Hello, ', ' data2 ': ' world!} ';
var evaljson=eval_r (' (' +jsondata+ ') ');
var jsonparsejson=json.parse (Jsondata);
This converts the Jsondata JSON-formatted string into a JSON object.
The differences are as follows:
var value = 1;
var jsonstr = ' {' data1 ': "Hello", "data2": ++value} ';
var data1 = Eval_r (' (' +jsonstr+ ') '); Console.log (DATA1);//This value is 2vardata2=json.parse (JSONSTR); Console.log (DATA2);//Error
You can see the results of the control output, the first eval_r () successfully executed, the second error.
As can be seen from the above example, eval will execute the code in the string when parsing the string (the consequences are pretty bad), as in the previous example, the value of the original values has been changed by using Eval to parse a JSON string.
High-performance JavaScript says: warning: With JSON and Eval, it's important to note that using eval in code is dangerous, especially if you use it to execute third-party JSON data, which may contain malicious code. Use the Json.parse () method whenever possible to parse the string itself. This method captures syntax errors in JSON and allows you to pass in a function that filters or transforms the parsing results. If this method is for Firfox 3.5, IE8, and Safari 4 native support. Most JavaScript class libraries contain JSON parsing code that calls the native version directly, and if not natively supported, a slightly less powerful non-native version is called for processing.
The difference between Json.parse and eval (RPM)