JSON data parsing is an important point

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.