Comparison and Analysis of Two Methods for parsing Json in Ajax _ json-js tutorial

Source: Internet
Author: User
Here we will introduce the comparison and analysis of the two methods for parsing Json in Ajax, which is very practical. This article is for study notes and is a beginner's article. Thank you for your advice! Eval (); // This method is not recommended

JSON. parse (); // recommended method

I. Differences between the two methods

First, initialize an object in json format:

Var jsonDate = '{"name": "Stephen Chow", "age": 23} 'var jsonObj = eval (' + jsonDate + ')'); // eval (); Method var jsonObj = JSON. parse (jsonDate); // JSON. parse (); Method

Then, call the following command in the console:

Console. log (jsonObj. name); // either of the two methods can be entered correctly.

So what are the differences between the two methods? (Next, let's change the code a little. The blue font is the modified part)

Var jsonDate = '{"name": alert ("hello"), "age": 23} 'var jsonObj = eval (' + jsonDate + ')'); // eval (); Method console. log (jsonObj. age); // The system will first execute "alert" to output "hello" and then output 23

For "JSON. parse:

Var jsonDate = '{"name": alert ("hello"), "age": 23}' var jsonObj = JSON. parse (jsonDate); // JSON. parse (); Method cosole. log (jsonobj. age) // an error is reported, which indicates that the string is invalid.

Summary: The "eval ();" method does not determine whether the string is valid during parsing, And the js method in the json object will also be executed, which is very dangerous; and "JSON. parse (); "the advantages of this method are unnecessary. We recommend this method. (You can test it on the console)

Ii. Expansion Problems

The Code is as follows:


Var jsonDate = '{"name": "Stephen Chow", "age": 23 }'

We can see that during the above test, the quotation marks enclosed in curly braces are marked in red all the time. This pair of quotation marks is critical but often ignored, because "eval ();" and "JSON. parser (); "the parameters of the two methods only accept strings, that is, only strings can be parsed !!

I don't have to think about it. If we do not add quotation marks during initialization, it is an object. js can directly obtain the attributes and methods of the object; why do I need to quote it into a string and then use "eval ();" or "JSON. parse (); "analysis, isn't it neither environmentally friendly nor efficient?

The reason is very simple: the front-end can only be provided to the background in the string data format, the background returns to the foreground to see what data format is returned, the string must be parsed and then used.

(This small problem is usually ignored and not very concerned. I was curious because I didn't know enough about the background. I threw this problem out and hoped it would be helpful to friends who were unfamiliar with the background. I knew how it would naturally deepen my memory afterwards, will not be missed during the development process)

The above is all the content of this article. I hope you will like it.

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.