JSON parsing why eval ("(" +data+ ")")

Source: Internet
Author: User

Refer to Blog Park Blog Eval parsing json note points

The front desk uses jquery to submit data, the background from the jquery request to get parameters, and then go to query the database, the query results are packaged as JSON object return, in jquery to parse the JSON results, but the resolution is a little puzzled: why the eval parsing with a pair of brackets in parentheses?


---------------the answer from Blog Park blog--------------

Why to Eval here to add "("("+data+");//"?

The reason is that eval itself is a problem. Since JSON is started and ended in the way "{}", in JS, it will be

is treated as a block of statements, so it must be coerced into an expression.

The purpose of the parentheses is to force the Eval function to force the expression in parentheses when it is handling JavaScript code

(expression) is converted to an object instead of being executed as a statement (statement). Give an example, for example, to

Elephant character polygon Amount {}, if no outer brackets are added, Eval will recognize the braces as the beginning of the JavaScript block and

End Mark memory, then {} will be considered as executing an empty statement. so the following two execution results are different:


Alert (eval ("{}");//return undefined


Alert (eval ("({})");//return Object[object]

For this kind of writing, in JS, you can see everywhere.

such as: (function ()) {} (); Do the closure operation and so on.


alert (dataObj.root.length);//number of sub-objects of output root

$.each (Dataobj.root,fucntion (Idx,item) {


if (idx==0) {


return true;
}
Output the name and value of each root sub-object
Alert ("Name:" +item.name+ ", Value:" +item.value ");
})
Note: for general JS-generated JSON objects, you only need to replace the $.each () method with the For statement, and the other is unchanged.






At the same time, from the blog to learn:

In JS, the JSON string is parsed into JSON data format in two ways: eval () and function object completion


First type: eval ()

The data types obtained by jquery asynchronously are based on the--json object and string, respectively, and describe the result processing methods obtained by the two methods.

1. For the JSON string returned by the server, if the jquery asynchronous request does not make a type description, or is accepted as a string, then an object processing is required, either in a cumbersome way or by placing the string in eval (). This is also a good way to get JSON objects in a normal javascipt way.

2. For the JSON string returned by the server, if the jquery asynchronous request sets the type (typically this configuration property) to "JSON", or uses the $.getjson () method to get the server back, then the eval () method is not required. Because the result is already a JSON object, just call the object directly, here $.getjson method as an example to illustrate the data processing method:
$.getjson ("http://www.phpzixue.cn/", {param: "Gaoyusi"},function (data) {//The data returned here is already a JSON object
The following other actions are the same as the first case
$.each (Data.root,function (Idx,item) {
if (idx==0) {
Return true;//with Countinue, returns false with break
}
Alert ("Name:" +item.name+ ", Value:" +item.value ");
});
});
In particular, it is important to note that the eval () method in mode 1 is the dynamic execution of strings (possibly JS scripts) , which can easily cause system security problems. So you can use some third-party client script libraries that circumvent eval (), such as JSON in JavaScript, which provides a script library of no more than 3k.



The second uses the function object to completeits typical application is the success of the Ajax method in jquery for the parsing of the returned data

var json= ' {' name ': ' CJ ', ' Age ': 18} ';
data = (New Function ("", "Return" +json)) ();

The data at this point is the one that will parse into a JSON object.



JSON parsing why eval ("(" +data+ ")")

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.