Problems encountered with Json.parse when doing projects

Source: Internet
Author: User

When I was doing the project today, I encountered a problem using json.parse to convert strings into JSON data.

Examples are as follows:

1 var jsonstr = ' sex ': "Male", "info": "I am a good person"};
2 Json.parse (JSONSTR);

The results of the operation are as follows:

Jsonstr There are two backslashes, json.parse in the implementation of the error, we use Eval to perform nature is not an error, the results of the operation are as follows:

Let's take a look at the following three lines of code:

1 var jsonStr1 = ' {sex: "Man", info: "I am a good person"};
2 var jsonStr2 = "{' Sex ': ' Male ', ' info ': ' I am a good person '}";
3 var jsonStr3 = ' sex ': "Male", "info": "I am a good person"};

The results of the operation are as follows:

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/

From the results of the operation can be seen: only JSONSTR3 This format of the JSON string Json.parse can be correctly converted, with Eval to deal with nature is no problem, the results of the operation are as follows:

Summarize

Json.parse is the specification proposed by ES5, the compatible view is as follows: (url:http://kangax.github.io/es5-compat-table/)

JSON is compatible from IE8 in IE, other types of browsers are almost all compatible, and the JSON API uses a lower threshold, which most browsers can use directly,

Some of the Third-party JS plug-ins processed by JSON are also based on the JSON native API, and the JSONSTR1,JSONSTR2,JSONSTR3 three formats are only JSONSTR3 to perform correctly

It should be JSON. The format requirements of the JSON string are stricter and more canonical; The JSON string is not understood if it has two backslashes.

The distinction and pros and cons of Json.parse and Eval are quoted in the high Performance JavaScript book:

Warning: For JSON and Eval, it is very dangerous to use eval in your code, especially if you use it to execute third party JSON data, which may contain malicious code,

Use the Json.parse () method whenever possible to resolve the string itself. This method captures the syntax errors in JSON and allows you to pass in a function that filters or transforms the parsed results.

If this method is ready for Firfox 3.5, IE8 and Safari 4 native support. Most JavaScript class libraries contain JSON parsing code that invokes the native version directly,

If there is no native support, a slightly less powerful version will be invoked to handle it.

Author: cnblogs Juntao

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.