Introduction to the attention points in jquery eval parsing JSON

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

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

2. Use the Function object for return parsing.

Use the eval function to parse, and use the each method of jquery to traverse

the method of parsing JSON data with jquery, as the transmission object of jquery asynchronous request, the result returned after the jquery request is a json object, what is considered here is that the server returns the form of JSON The form of a string is similar to the JSON object encapsulated by plugins such as JSONObject, and will not be described here.
Here first, the JSON string set is given. The string set is as follows: The

code is as follows:

code show as below:

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:'Yan'an City'},
{name:'6107',value:'Hanzhong City'},
{name:'6108',value:'Yulin City'},
{name:'6109',value :'Ankang City'},
{name:'6110',value:'Shangluo City'}
]
}";


Based on the data types obtained asynchronously by jquery - json objects and strings, the processing methods of the results obtained by the two methods are introduced respectively.

1. For the JSON string returned by the server, if the jquery asynchronous request has no type description, or is accepted as a string, then an objectification process is required. The method is not too troublesome, that is, put the string in eval() Execute once. This method is also suitable for obtaining json objects in ordinary javascipt methods. The following example illustrates:

var dataObj=eval("("+data+")");//Convert to json objects
Why do you need to add "("("+ to eval here data+")");//"?

The reason is: the problem of eval itself. Since json starts and ends with "{}", in JS, it will be treated as a statement block, so it must be converted into an expression.

The purpose of adding parentheses is to force the eval function to convert the parenthesized expression (expression) into an object when processing JavaScript code, rather than as a statement (statement). For example, for an object literal {}, if no outer brackets are added, eval will recognize the curly brackets as the start and end tags of a JavaScript code block, and {} will be considered as executing an empty statement. So the following two execution results are different:

code show as below:

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


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

Such as: (function()) {}(); When doing closure operations, etc.
-------------------------------------------------- ------------------------------

code show as below:

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


Note: For general js to generate json objects, you only need to replace the $.each() method with a for statement, and the others remain unchanged.

2. For the JSON string returned by the server, if the jquery asynchronous request sets the type (usually this configuration property) to "json", or uses the $.getJSON() method to obtain the server return, then the eval() method is not needed. , because the result obtained at this time is already a json object, you only need to call the object directly. Here, the $.getJSON method is used as an example to illustrate the data processing method:

code show as below:

$.getJSON("http://www.phpzixue.cn/",{param:"gaoyusi"},function(data){
//The data returned here is already a json object
//The following other operations are the same as the first Case
$.each(data.root,function(idx,item){
if(idx==0){
return true;//Same as countinue, return false with break
}
alert("name:"+item.name+",value :"+item.value);
});
});


It should be noted here that the eval() method in method 1 dynamically executes the string (may be a js script), which can easily cause system security problems. So you can use some third-party client-side script libraries that avoid eval(). For example, JSON in JavaScript provides a script library of no more than 3k.

The second way of parsing is to use the Function object to complete, its typical application is the parsing of the returned data data under the AJAX method in JQUERY, etc.

code show as below:

var json='{"name":"CJ","age":18}';
data =(new Function("","return "+json))();


The data at this time is a json object that will be parsed.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326992369&siteId=291194637