How jquery parses a JSON string

Source: Internet
Author: User
Tags json

jquery parses JSON data format:

Using this method, you must set the parameters in the AJAX request:
1 DataType: "JSON"
Get the data returned by the callback function and parse to get the value we want, see source code:

The code is as follows Copy Code

Jquery.ajax ({
Url:full_url,
DataType: "JSON",
Success:function (Results) {
alert (result.name);
} });

Typically, you can return JSON data from the background, and the front desk is given to jquery, haha!!

The jquery asynchronous request sets the type (typically this configuration property) to "JSON", or uses the $.getjson () method to get the server back, so it does not

The eval () method is needed because the result is already a JSON object, just call the object directly, and here the $.getjson method

Example says


Example 1

The code is as follows:

The code is as follows Copy Code
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 '}
]
}";

Jquery

The code is as follows Copy Code

$.getjson ("http://sanic.cnblogs.com/", {param: "sanic"},function (data) {
The data returned here is already a JSON object
The following other actions 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");
});
});


jquery Parsing JSON objects:

jquery provides another way to "Parsejson", which requires a standard JSON string and returns the generated JavaScript object. Let's see

See Grammar:

Data = $.parsejson (string);

See how it is applied to the actual development:

  code is as follows copy code

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.