How does jQuery parse Json strings (Json format/Json object) _ jquery

Source: Internet
Author: User
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 );
}
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.