The JSON data parsing method using jquery is used as the transmission object of jquery asynchronous requests. The result returned by the jquery request is a json object, which is considered in the form of a JSON string returned by the server, JSON objects encapsulated by plug-ins such as JSONObject are similar in that they are not described here. Here we first give a JSON string set. The string set is as follows: Notes for parsing JSON in eval
JSON strings are parsed into JSON data formats in JS. There are two methods:
1. Use the eval_r () function.
2. Use the Function object for return parsing.
Use the eval function to parse and use the jquery each method to traverse
The JSON data parsing method using jquery is used as the transmission object of jquery asynchronous requests. The result returned by the jquery request is a json object, which is considered in the form of a JSON string returned by the server, JSON objects encapsulated by plug-ins such as JSONObject are similar in that they are not described here.
Here we first provide a JSON string set, which is as follows:
The Code is as follows:
Var data ="
{
Root:
[
{Name: '1', value: '0 '},
{Name: '000000', value: 'beijing '},
{Name: '123', value: 'tianjin '},
{Name: '000000', value: 'shanghai '},
{Name: '000000', value: 'chongqing '},
{Name: '200', value: 'weinan City '},
{Name: '000000', value: 'yan'an '},
{Name: '000000', value: 'hanzhong '},
{Name: '200', value: 'yulin '},
{Name: '123', value: 'ankang City '},
{Name: '200', value: 'shangluo City '}
]
}";
Here, based on the data type obtained asynchronously by jquery-json object and string, we will introduce the Processing Methods of the obtained results in two ways.
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_r (). This method is also suitable for obtaining json objects in the common ccipt mode. The following is an example:
Var dataObj = eval_r ("(" + data + ")"); // convert to a json object
Why do you want to add "(" ("+ data +") "); //" 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_r ("{}"); // return undefined
Alert (eval_r ("({})"); // return object [Object]
For this method, we can see it everywhere in JS.
For example, (function () {} (); During the closure operation.
Alert (dataObj. root. length); // output the number of root sub-objects
$. 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 json objects, you only need to replace the $. each () method with the for statement.
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 to obtain the server response, because the result is already 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.phpzixue.cn/", {param: "gaoyusi"}, 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_r () method in method 1 dynamically executes the strings (which may be JavaScript scripts), which can easily cause system security problems. Therefore, you can use third-party client script libraries that circumvent eval_r (). For example, JSON in JavaScript provides a script library of no more than 3 kb.
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": "CJ", "age": 18 }';
Data = (new Function ("", "return" + json ))();
At this time, data is parsed into a json object.