Convert JSON object using eval () method in Javascript

Source: Internet
Author: User
<Script language = "JavaScript"> var user = '{name: "Zhang San", age: 23,' + 'address: {City: "Qingdao", ZIP: "266071"}, '+ 'email: "iteacher@haiersoft.com.cn",' + '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 provide a type description or is accepted as a string, an object processing is required. The method is not too troublesome, but the string is placed in eval ().This method is also suitable for obtaining JSON objects in the common ccipt mode. The following is an example:

VaR u = eval ('+ User + ')');

Why do you want to add ('+ User +') 'to eval?

The reason is: Problems with Eval itself. JSON starts and ends in the form of "{}". In JS, JSON is treated as a statement block. Therefore, it must be forcibly converted into an expression.

The purpose of parentheses is to force the eval function to forcibly convert the expressions in parentheses into objects when processing Javascript code, rather than executing them as statements. For example, if no outer brackets are added to the object literal {}, Eval identifies the braces as the start and end mark of the JavaScript code block, then {} is considered to have executed an empty statement. 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 (usually this Configuration Attribute) to "JSON", or uses $. the getjson () method does not need the eval () method.Because the result is a JSON object, you only need to call this object directly. Here, the $. getjson method is used as an example to describe the data processing method:
$. Getjson ("http://www.qk12333.com/", {Param: "jcuckoo"}, function (data ){
// The returned data is already a JSON object.
// The following operations are the same as the first case
$. Each (data. Root, function (idx, item ){
If (idx = 0 ){
Return true; // returns the same value as countinue, and returns the same value as break.
}
Alert ("name:" + item. Name + ", value:" + item. value );
});
});

Note that the eval () method in method 1 dynamically executes the strings (which may be JavaScript scripts), which can easily cause system security problems.Therefore, some third-party client script libraries that circumvent eval () can be used. For example, JSON in Javascript provides a script library of no more than 3 K.
The second method of parsing is to use the function object. Its typical application is to parse the data returned by success under the Ajax method in jquery.
VaR JSON = '{"name": "username", "Age": 28 }';
Data = (new function ("", "Return" + JSON ))();
At this time, data is parsed into a JSON object.

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.