JSON parse eval converts strings to JSON similarities and differences

Source: Internet
Author: User

SON (JavaScript Object Notation) is a lightweight format that uses text formats that are completely language-independent and ideal for exchanging formats. At the same time, JSON is a native JavaScript format, which means that it does not require any special APIs or toolkits to handle JSON in JavaScript and is very efficient.

The structure of JSON is as follows:

The collection of "name/value" pairs (A collection of name/value pairs). In different languages, it is understood as the object, record, structure (struct), Dictionary (dictionary), hash table (keyed list), or the relational group (associative), or the Array).
The sequence of values (an ordered list of values). In most languages, it is understood as a number (array)
A standard JSON format:

{"Name": "Jifeng", "Company": "Taobao"} In general, JSON is relatively easy to understand and use, but there are a lot of traps, if you are not aware of the easy to fall into, this article is mainly to tell it easy to make mistakes in the place.



One, the analytic method of JSON

There are two kinds of parsing methods for JSON: 1. eval (); 2.json.parse (). The method of use of the body is as follows

var jsonstr = ' {' name ': ' Jifeng ', ' Company ': ' Taobao '} ';//eval functionvar Evaljson = eval (' (' + jsonstr + ') ');//Json.parse Functionvar Jsonparsejson = Json.parse (JSONSTR);

Since both can reach the result of parsing JSON, what is the difference between those two? I'll use one of the following examples to illustrate:

1. Use the Eval method:

var parse_json_by_eval = function (str) {return eval (' (' +str+ ') ');} var value = 1;var Jsonstr = ' {' name ': ' Jifeng ', ' Company ': ' Taobao ', ' Value ': ++value} '; var json1 = Parse_json_by_eval (    JSONSTR); Console.log (Json1); Console.log (' Value: ' + value); Perform the following results:

{name: ' Jifeng ', Company: ' Taobao ', Value:2}value:2 2. Using the Json.parse method

var parse_json_by_json_parse = function (str) {return json.parse (str);} Value = 1;var Jsonstr = ' {"name": "Jifeng", "Company": "Taobao", "Value": ++value} '; var json2 = Parse_json_by_json_parse (     JSONSTR); Console.log (Json2); Console.log (value); Perform the following results:

You can't do it smoothly.



The former can perform the work smoothly, and modify the value of the global change of value, and the latter is wrong. As can be seen from the above example, eval will execute the code in the string when parsing the string (this is a bad result), as in the previous example, the value of the original values is changed by using Eval to parse a JSON string. The book of high-performance JavaScript states:

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.




One, ' \ ' influence on JSON

Since JSON is parsed from a string, we first understand the effect of ' \ ' on strings before we understand ' \ ' to the JSON. This is particularly strong in JavaScript ' and ' when representing strings, such as ' a ' = ' a '

It is well known that because the string type contains a literal literal, such as ' \ n ' for a line, ' \b ' denotes a space, and so on, it is necessary to use this special character to represent the He say "hello", that is, "he say \" "Hello \". In Chrome, Console.log () can be seen clearly.
Http://dl.iteye.com/upload/attachment/0062/0247/1e643d33-828d-3665-80f7-cb5adaf7b93d.jpg


and Json.parse () is the real definition of the string parsing, to indicate that \ must be represented by "\\\\", with the following image:

Http://dl.iteye.com/upload/attachment/0062/0249/dae6c992-ff46-3b6c-8104-af1c6f583271.jpg

In JSON, you have to be careful, it's the easiest place in JSON to make mistakes.

In a small episode: When I know in JSON parsing need to use "\\\\" when the "\", you use Json.parse (' {"A": "a\\b"} "), there is no wrong, I understand wrong, the heart of the friend should see, ' \b ' itself is a conversion character, So the first ' \ ' In this is used to transform the second ' \ ', the string is programmed ' a\b ' (' \b ' is a conversion character), so Json.parse () can still be parsed.

There's a bit of a detour to this concept, and the reader needs to think about it, and it takes a longer time to think about it.







Supplemental information:

Online parsing JSON web site: http://json.parser.online.fr/

JSON parse eval converts strings to JSON similarities and differences

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.