The difference between json.parse () and eval () parsing JSON in JavaScript _javascript techniques

Source: Internet
Author: User

This example describes the difference between json.parse () and eval () parsing json in JavaScript. Share to everyone for your reference, specific as follows:

JSON (JavaScript Object notation) is a lightweight data format that takes a completely language-independent text format and is an ideal data interchange format. Also, JSON is the native format of JavaScript, which means that JSON data is processed in JavaScript

Basic format:

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

Calling Method Jsondata.data1,jsondata.data2

Many JSON data is stored in an array

var jsondata=[
 {"name": "Lilei", "age": \, "Sex": "Male"},
 {"name": "Hanmei", "Age": "Sex": "Famale"}
]

Call Method Jsondata[0].name,jsondata[1].sex

Overall, JSON is relatively easy to understand and use, but at the same time there are a lot of traps, if not pay attention to easily fall in.

The parsing method of JSON

There are two ways to parse JSON: eval () and Json.parse (), using the following methods:

var jsondata = ' {data1 ': "Hello,", "data2": "world!}";
var evaljson=eval (' (' +jsondata+ ') ');
var jsonparsejson=json.parse (Jsondata);

This converts the string jsondata this JSON format into a JSON object.

The difference is as follows:

var value = 1;
var jsonstr = ' {data1 ': ' Hello ', ' data2 ': ++value} ';
var data1 = Eval_r (' (' +jsonstr+ ') ');
Console.log (DATA1)//Then value is 2
var data2=json.parse (jsonstr);
Console.log (DATA2)/error

You can see the results of control transmission, the first eval () successfully executed, the second error. It is obvious from the previous example thateval executes 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 was changed by using Eval to parse a JSON string.

"High performance JavaScript" (Download address: http://www.jb51.net/books/36680.html) A book that says:

WARNING: What you need to be aware of about JSON and Eval is: using eval in your code is dangerous, especially if you use it to perform third-party JSON data (which may contain malicious code), using the Json.parse () method whenever possible to parse 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 and, without native support, invokes a slightly less powerful, non-native version to handle.

More about JavaScript related content to view the site topics: "JavaScript in the JSON Operation tips Summary", "JavaScript switching effects and techniques summary", "JavaScript Search Algorithm Skills summary", " JavaScript animation effects and tips Summary, "JavaScript Error and debugging skills Summary", "JavaScript data structure and algorithm skills summary", "JavaScript traversal algorithm and Skills summary" and "JavaScript Mathematical operation Usage Summary"

I hope this article will help you with JavaScript programming.

Related Article

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.