Reprint--json

Source: Internet
Author: User

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.

The structure of the JSON is as follows:

    • A collection of name/value pairs (A collection of name/value pairs). In different languages, it is understood as objects (object), record (record), structure (struct), Dictionary (dictionary), hash table (hash table), keyed list (keyed list), or associative array (associative array).
    • The sequence of values (an ordered list of values). In most languages, it is understood as an array (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 not pay attention to easily fall in, this article is mainly to tell it easy to make people wrong place.

One, the analytic method of JSON

There are two ways to parse JSON: 1. eval (); 2.json.parse (). Here's how to use it

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

Since both can achieve the result of parsing JSON, what is the difference between the two? I 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);

Execution Result:

{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);

Execution Result:

Unable to execute successfully, error

The former can execute smoothly, and modify the value of the global variable, and the latter 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," a book that says:

Warning: With regard to JSON and eval, it is very dangerous to use eval in your code, especially if you are using it to execute third-party JSON data (which might contain malicious code), and use the Json.parse () method to parse the string itself whenever possible. 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 effect of the ' \ ' on JSON

Since JSON objects are parsed from strings, we first understand the effect of ' \ ' on strings before we understand the effects of ' \ ' on JSON. In particular, it is emphasized that the ' and ' in JavaScript is equivalent in representing a string, such as ' a ' = ' a '

It is well known that because the string data type contains an escape character, such as ' \ n ' means a newline, ' \b ' denotes a space, and so on, it needs to be represented by the special character of ' \ ' when he say "Hello", that is, he say \ "Hello \" ". In Chrome, Console.log () can be seen clearly.

and Json.parse () is the real meaning of the string to parse, to indicate that \ must be used "\\\\", specifically see:

You have to be very careful when it comes to "\" in JSON, which is the most error-prone part of JSON.

In the incidentally an episode: When I know in JSON parsing need to use "\\\\" when the "\", you use Json.parse (' {"A": "a\\b"} '), even if there is no error, I understand wrong, careful friend should see, ' \b ' itself is an escape character, So the first ' \ ' Here is used to escape the second ' \ ', so that the string is programmed ' a\b ' (' \b ' is an escape character), so Json.parse () can still be resolved smoothly.

The concept is still a bit around, the reader needs to think more about it, it was also spent a long time to think about this problem.

Reprint--json

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.