The eval () method in JavaScript transforms a JSON object

Source: Internet
Author: User

Original: The eval () method in JavaScript transforms a JSON object

<script language= "JavaScript" >    var user = ' {name: ' Zhang San ', age:23, ' + ' address:{city: "Qingdao", Zip: "266071"}, ' + ' Email: "[email protected]", ' + ' showinfo:function () {' + ' document.write ("Name:" +this.name+ "<br/>"); ' + ' document.write ("Age: +this.age+" <br/> "); ' + ' document.write ("Address:" +this.address.city+ "<br/>"); ' + ' document.write ("Zip code:" +this.address.zip+ "<br/>"); ' + ' document.write ("e-mail:" +this.email+ "<br/>");} }‘; var u = eval (' (' +user+ ') '); U.showinfo ();</script>


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, as illustrated below:

var u = eval (' (' +user+ ') ');

Why should Eval be added here (' (' (' +user+ ') ')?

The reason is that eval itself is a problem. Since JSON starts and ends in the form of "{}", in JS, it 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 to be converted to an object while processing the JavaScript code, rather than being executed as a statement (statement). For example, if the object literal {} is not enclosed, then eval will recognize the curly brace as the start and end tag of the JavaScript block, and {} will be considered an empty statement. So the following two execution results are different:
Alert (eval ("{}");//return undefined
Alert (eval ("({})");//return Object[object]

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 the $.getjson method as an example to illustrate the data processing method:
$.getjson ("http://www.qk12333.com/", {param: "Jcuckoo"},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 parsing method is to use the function object to complete, its typical application is in jquery in the Ajax method of success and so on the return data of the parsing
var json= ' {' name ': ' UserName ', ' Age ': 28} ';
data = (New Function ("", "Return" +json)) ();
The data at this point is the one that will parse into a JSON object.

The eval () method in JavaScript transforms a JSON object

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.