The difference between JSON. parse () and eval () parsing json in javascript is: json. parsejson

Source: Internet
Author: User

The difference between JSON. parse () and eval () parsing json in javascript is: json. parsejson

This article describes the differences between JSON. parse () and eval () parsing json in javascript. We will share this with you for your reference. The details are as follows:

JSON (JavaScript Object Notation) is a lightweight data format that uses a completely language-independent text format and is an ideal data exchange format. JSON is a Javascript native format, which means processing JSON data in javascript

Basic Format:

varjsonData='{"data1":"Hello,","data2":"world!"}'

Call method jsonData. data1, jsonData. data2

Store a lot of json data into Arrays

var jsonData=[ {"name":"LiLei","age":19,"sex":"male"}, {"name":"HanMei","age":18,"sex":"famale"}]

Call MethodJsonData [0]. name, jsonData [1]. sex

In general, json is relatively easy to understand and use, but there are many traps at the same time, if you do not pay attention to it, it is easy to fall.

Json Parsing Method

Json is parsed in two ways: eval () and JSON. parse (). The method is as follows:

var jsonData = '{"data1":"Hello,", "data2":"world!}';var evalJson=eval('('+jsonData+')');var jsonParseJson=JSON.parse(jsonData);

In this way, the json string jsonData is converted into a JSON object.

The differences between the two are as follows:

Var value = 1; var jsonstr = '{"data1": "hello", "data2": ++ value }'; var data1 = eval_r ('+ jsonstr +'); console. log (data1); // The value is 2var data2 = JSON. parse (jsonstr); console. log (data2); // Error

The result of the control output table is displayed. The first eval () is successfully executed, and the second error is reported. As shown in the preceding example,When parsing a string, eval will execute the code in the string (the consequence is rather bad ),In the preceding example, the original value is changed due to parsing a json string using eval.

High Performance Javascript (site: http://www.bkjia.com/books/36680.html) book that points out:

Warning JSON and eval:It is very dangerous to use eval in code, especially when using it to execute third-party JSON data (which may contain malicious code), try to use the JSON. parse () method to parse the string itself. This method can capture syntax errors in JSON and allow you to input a function to filter or convert the parsing results. If this method is supported by Firfox 3.5, IE8, and Safari 4 native. Most javascript class libraries contain JSON Parsing Code that directly calls the native version. Without native support, it will call a slightly less powerful non-native version for processing.

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.