in JS to parse the JSON string into JSON data format, generally there are two ways: using the eval () function, using the function object for return parsing, here is an example, interested friends can refer to the followingin JS, the JSON string is parsed into a JSON data format, typically in two ways:
1. One for using 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
using jquery to parse JSON data as a transport object for jquery asynchronous requests, the result of the jquery request is the JSON object, which is all about the server returning the form of a string in JSON form. For JSON objects that are encapsulated with plug-ins such as Jsonobject, this is also the same thing, and is no longer explained here.
Here we first give a set of JSON strings, the string set as follows:
The code is as follows:
Copy the Code code as follows:
var data= "
{
Root:
[
{name: ' 1 ', Value: ' 0 '},
{name: ' 6101 ', Value: ' Beijing '},
{name: ' 6102 ', Value: ' Tianjin '},
{name: ' 6103 ', Value: ' Shanghai City '},
{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 obtained by jquery asynchronously,--json object and string, and describes how the results are handled in two ways.
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 dataobj=eval ("(" +data+ ")");//Convert to JSON object
Why to Eval here to add "(" ("+data+");//"?
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:
Copy the Code code as follows:
Alert (eval ("{}");//return undefined
Alert (eval ("({})");//return Object[object]
for this kind of writing, in JS, you can see everywhere.
such as: (function ()) {} (), when doing a closure operation, etc.
--------------------------------------------------------------------------------
Copy the Code code as follows:
alert (dataObj.root.length);//number of sub-objects of output root
$.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-generated JSON objects, you only need to replace the $.each () method with the For statement, and the other is unchanged.
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 $.getjson method as an example to illustrate the data processing method:
Copy the Code code as follows:
$.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;//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
Copy the Code code as follows:
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.
Introduction to the notes in the jquery eval parsing json