I learned a little bit about JSON data today. Because it is usually ignored. So take a note and write it down:
There are two kinds of parsing methods for JSON: 1. eval (); 2.json.parse ().
Use of the first type of eval
var Evaljson = eval (' (' + jsonstr + ') ');
The second kind of json.parse
var Jsonparsejson = Json.parse (JSONSTR);
Focus!!! The difference between the two:
var parse_json_by_eval = function (str) {
Return eval (' (' + str + ') ');
}
var parse_json_by_json_parse = function (str) {
return Json.parse (str);
}
var value = 1;
var jsonstr = ' {' id ': ' 1 ', ' name ': ' Root ', ' value ': ++value} ';
var json1 = Parse_json_by_eval (JSONSTR);
Console.log (Json1);
Console.log (' Value: ' + value);
Execute results: {name: ' Jifeng ', Company: ' Taobao ', value:2} value:2
var json2 = Parse_json_by_json_parse (JSONSTR);
Console.log (Json2);
Console.log (' The second type of value: ' + value ');
Execute results: Error cannot be executed.
Eval will execute the code in the string (the result is pretty bad, unsafe!)
warning: For JSON and Eval, it's important to note that using eval in a code is dangerous, especially if you use it to perform a third-party JSON (which may contain a malicious code), and you can parse the string itself using the Json.parse () method. This method captures the language bug in JSON and allows you to pass in a function that uses filtering or conversion to parse the results. If this method is ready for Firfox 3.5, IE8 and Safari 4 native support. The JSON parsing code included in most JavaScript repositories is directly tuned to the native version, and if there is no native support, a slightly less powerful non-native version is used.
The effect of ' \ ' on JSON:
Because the string data type contains the Json.parse character, such as ' \ n ' for the line, and the true definition of the string is parsed, the \ must be represented by "\\\\"
(Json.parse (' {"A": "a\\b"} ')) so a\\b is OK.
Original link: http://literary-fly.iteye.com/blog/1343268
JSON data parsing is an important point