You can use jquery or js to parse json data. Here we will share with you the jquery json string parsing method. One is to parse Json format and the other is to parse Json objects, interested friends can learn that json data is a commonly used small data exchange in real time. It can be parsed using jquery or js, the following describes how jquery parses json strings.
I. jQuery parses Json data format:
To use this method, you must set parameters in the Ajax request:
1 dataType: "json"
Obtain the data returned by the callback function and parse it to get the desired value. See the source code:
The Code is as follows:
JQuery. ajax ({
Url: full_url,
DataType: "json ",
Success: function (results ){
Alert (result. name );
}});
Normally, you can return JSON data from the background, and the foreground will be handed over to jQuery. Haha !!
Jquery asynchronous requests set the type (usually this Configuration Attribute) to "json", or use the $. getJSON () method to obtain the server's return.
The eval () method is required, because the result is already a json object. You only need to call this object directly. Here, the $. getJSON method is used
For example
Example 1
The Code 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 '}
]
}";
Jquery
The Code is as follows:
$. GetJSON ("http://sanic.cnblogs.com/", {param: "sanic"}, 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 );
});
});
Ii. parse Json objects using jQuery:
JQuery provides another method "parseJSON", which requires a standard JSON string and returns the generated JavaScript Object. Let's see
View Syntax:
Data = $. parseJSON (string );
Let's see how it is applied to actual development:
The Code is as follows:
JQuery. ajax ({
Url: dataURL, success: function (results ){
Var parsedJson = jQuery. parseJSON (results );
Alert (parsedJson. name );
}
});