Home>

Parse the json string into json data format in js,There are two general ways:

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

2. Use the function object for return parsing.

Use eval function to parse,And use jquery's each method to traverse

Method to parse json data with jquery,As the transmission object of jquery asynchronous request,The result returned by the jquery request is a json object. What we are considering here is the form of the string returned by the server as json.For json objects packaged with plugins such as jsonobject, this is also similar.No further explanation here.

Here first gives the json string set,The string set 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 City"},{name:"6106", value:"Yan'an City"},{name:"6107", value:"Hanzhong"},{name:"6108", value:"Yulin City"},{name:"6109", value:"Ankang"},{name:"6110", value:"Shangluo City"}
]
} ";

Here is based on the data type asynchronously obtained by jquery-json object and string,The results processing methods obtained by the two methods are introduced separately.

1. For the json string returned by the server, if the jquery asynchronous request does not specify the type,Or accept it as a string,Then you need to do an objectization process,Way is not too much trouble,This string is executed once in eval ().This method is also suitable for obtaining json objects in ordinary javascipt mode. The following examples illustrate:

var dataobj=eval ("(" + data + ")");//Convert to json object
Why does red rose eval add "(" ("+ 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 forced into an expression.

The purpose of adding parentheses is to force the eval function to force the expression in parentheses into an object when processing javascript code.It is not executed as a statement. for instance,For example, the object literal {}, if you do not add outer brackets,Then eval will recognize the braces as the start and end tags of the javascript code block,Then {} will be considered as executing an empty statement.So the following two execution results are different:

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

For this writing,You can see it everywhere in js.

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

alert (dataobj.root.length);//Output the number of child objects of root
$.each (dataobj.root, fucntion (idx, item) {
if (idx == 0) {
return true;
}
//Output the name and value of each root child object
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,Others are unchanged.

2. For the json string returned by the server, if the jquery asynchronous request sets the type (generally this configuration property) to "json" or use the $.getjson () method to get the server returnThen the eval () method is not needed, because the result obtained at this time is already a json object, just call the object directly,Here takes 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 operations are the same as the first case
$.each (data.root, function (idx, item) {
if (idx == 0) {
return true;//Same as countinue, return false and break
}
alert ("name:" + item.name + ", value:" + item.value);
});
});

It is particularly important to note here that the eval () method in method 1 dynamically executes the string (possibly a JS script), which can easily cause system security problems.So you can use some third-party client script libraries that circumvent eval ().For example, json in javascript provides a script library up to 3k.

The second way of parsing is to use the function object to complete,Its typical application is the analysis of the returned data in the success of the ajax method in jquery.

var json="{" name ":" cj "," age ":18}";
data=(new function ("", "return" + json)) ();

At this point, the data is a json object.

  • Previous PHP using PHPexcel to import and export data
  • Next Interpretation of common regular expression knowledge points and interpretation of valid numbers and mobile phone number mailbox