Parsing of JSON strings

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

Jsonstr = ' {' name ': ' Jifeng ', ' Company ': ' Taobao '} '; Evaljson = eval (' (' + jsonstr + ') '); 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:

Parse_json_by_eval = (str) {eval (' (' +str+ ') ');} value = 1; Jsonstr = ' {' name ': ' Jifeng ', ' Company ': ' Taobao ', ' Value ': ++value} '; 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

Parse_json_by_json_parse = (str) {json.parse (str);} Value = 1; Jsonstr = ' {' name ': ' Jifeng ', ' Company ': ' Taobao ', ' Value ': ++value} '; 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.

Parsing of JSON strings

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.