This article illustrates the JavaScript transformation and parsing JSON method. Share to everyone for your reference, specific as follows:
The JSON format data is as follows:
var json = {' jquery ': [{' id ': ' 1 ', ' type ': ' asp.net ', ' title ': ' JSON full resolution}]}
alert (json.jquery[0].id);
alert (json.jquery[0].type);
alert (json.jquery[0].title);
JavaScript parses JSON data:
Window.onload = function () {
var json = {China]: [{' City ': ' Shanghai ', ' value ': ' 1 '},{' city ': ' Nanjing ', ' Value ': ' 2 '},{' City ': ' Hangzhou ', ' Value ': ' 3 '},{' City ': ' Wuhan ', ' Value ': ' 4 '}] '
var datas = eval (json. );
for (var i = 0; i < datas.length i++)
{
alert (datas[i). City);
alert (datas[i].value);
}
}
Add: JQuery Parse JSON method:
Use the Eval function to parse the jquery each method 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:
var data= '
{
root:
[
{name: ' 1 ', Value: ' 0 '},
{name: ' 6101 ', Value: ' Beijing '},
{name: ' 6102 ', Value: ' Tianjin '},
{name: ' 6103 ', Value: ' Shanghai '},
{name: ' 6104 ', Value: ' Chongqing '},
]
} ';
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.
Eval ()
For a 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:
Convert to JSON object
var dataobj=eval ("(" +data+ ")");
Why do I have to eval here to add ("(" +data+ ")"); It?
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:
return undefined
alert (eval ("{}");
Return Object[object]
alert (eval ({}));
For this type of writing, in JS, you can see everywhere. such as: (function ()) {} (); When doing closure operation.
The number of child objects of the output root
alert (dataObj.root.length);
$.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";
})
For a generic JS to generate a JSON object, simply replace the $.each () method with the For statement, the other unchanged.
The JSON string returned by the server
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, and here take the $.getjson method as an example to illustrate the data processing method:
$.getjson ("http://www.xxxx.com/", {param: "Gaoyusi"},function (data) {
//The data returned here is already a JSON object
//The following other operations with the first case
$.each (data.root,function (idx,item) {
if (idx==0) {
//same countinue, return false with break returns
true;
}
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 parsing is done using a function object, and its typical application is the success of the Ajax method in jquery, and so on, to the parsing of the returned 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.