JS parsing json using eval analysis _javascript tips

Source: Internet
Author: User
Tags opening and closing tags

This paper analyzes the considerations of JS parsing json using eval in detail. Share to everyone for your reference, specific as follows:

There are generally two ways to parse a JSON string into a JSON data format in JS:

1. One is to use the Eval () function.

2. Use function objects to perform return parsing.

Use the Eval function to parse, and use each of the jquery methods to traverse the

The method of parsing JSON data in jquery, as the transmission object of the jquery asynchronous request, returns the result of the jquery request as a JSON object, taking into account the form of a string returned by the server in JSON form. The same is the case with JSON objects encapsulated by plug-ins such as Jsonobject, which are no longer described here.

Here we first give a set of JSON strings, and the string set is as follows:

The code is as follows:

var data= '
{
root:
[
{name: ' 1 ', Value: ' 0 '},
{name: ' 6101 ', Value: ' Beijing '},
{name: ' 6102 ', Value: ' Tianjin '},
{name: ' 6103 ', Value: ' Shanghai '},
{name: ' 6104 ', Value: ' Chongqing '},
{name: ' 6105 ', Value: ' Weinan '},
{name: ' 6106 ', Value: ' Yanan '},
{name: ' 6107 ', Value: ' Hanzhong '},
{name: ' 6108 ', Value: ' Yulin '},
{name: ' 6109 ', Value: ' Ankang '},
{name: ' 6110 ', Value: ' Shangluo '}
]
};

This is based on the data type--json object and string that jquery asynchronously obtains, and describes the result processing methods obtained in two ways respectively.

1. For the JSON string returned by the server, if the jquery asynchronous request does not have a type description or is accepted as a string, it is necessary to do an object-by-case, either in a cumbersome way, or to put the string in eval () once. This is also a good way to get JSON objects in a normal Javascipt way, as illustrated in the following example:

var dataobj=eval ("(" +data+) "); Convert to JSON object
Red rose Why do you want to eval here to add" ("(" +data+ "));/"?

The reason is that eval itself is a problem. Since JSON starts and ends with "{}", in JS it is treated as a block of statements, so it must be coerced into an expression.

The purpose of parentheses is to force the Eval function to force an expression in parentheses (expression) into an object when processing JavaScript code instead of executing as a statement (statement). For example, an object literal {}, if the outer bracket is not added, Eval recognizes the curly braces as the opening and closing tags of the JavaScript code block, then {} will be considered to have executed an empty statement. So the following two execution results are different:

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

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

such as: (function ()) {} (); When doing closure operation.

alert (dataObj.root.length);//output root number of child objects 
$.each (dataobj.root,fucntion (idx,item) { 
if (idx==0) { 
return true; 
} 
Output the name and value 
alert ("Name: +item.name+", Value: "+item.value) of each root child"; 
})

Note: For the general JS generation JSON object, only need to replace the $.each () method with the For statement, the other unchanged.

2. For the JSON string returned by the server, the eval () method is not required if the jquery asynchronous request sets the type (typically this configuration property) to "JSON" or uses the $.getjson () method to get the server back. Because the result is already a JSON object, just call the object directly, and here take the $.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, return false with Break 
} 
alert ("Name: +item.name+", Value: "+item.value);}"; 
});

In particular, it is important to note that the eval () method in mode 1 executes the string (possibly the JS script) dynamically, which can easily cause a system security problem. So you can use a few 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 way to do this is to use a function object to do it, and it's typical application is the success of the Ajax method in jquery to the return data

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

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

I hope this article will help you with JavaScript programming.

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.