JSON Parsing Method in jquery, jqueryjson Parsing

Source: Internet
Author: User

JSON Parsing Method in jquery, jqueryjson Parsing

All considerations are the form of JSON strings returned by the server. JSON objects encapsulated using JSONObject and Other plug-ins are similar in this case. I will not describe them here.

Copy codeThe Code is as follows:
Var data ="
{
Root:
[
{Name: '1', value: '0 '},
{Name: '000000', value: 'xi'an '},
{Name: '000000', value: 'tongchuan '},
{Name: '200', value: 'baoji City '},
{Name: '000000', value: 'xianyang '},
{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 '}
]
}";

Here, based on the data type obtained asynchronously by jquery-json object and string, we will introduce the Processing Methods of the obtained results in two ways.

1. for the JSON string returned by the server, if the jquery asynchronous request does not provide a type description or is accepted as a string, an object processing is required. The method is not too troublesome, but the string is placed in eval (). This method is also suitable for obtaining json objects in the common ccipt mode. The following is an example:

Copy codeThe Code is as follows:
Var dataObj = eval ("(" + data + ")"); // convert to a json object
Alert (dataObj. root. length); // output the number of root sub-objects
$. Each (dataObj. root, fucntion (idx, item ){
If (idx = 0 ){
Return true;
}
// Output the name and value of each root sub-Object
Alert ("name:" + item. name + ", value:" + item. value );
})

Note: for General js json objects, you only need to replace the $. each () method with the for statement.

2. for the JSON string returned by the server, if the jquery asynchronous request sets the type (usually this Configuration Attribute) to "json", or uses $. the getJSON () method does not need the eval () method to obtain the server response, because the result is already a json object. You only need to call this object directly. Here, $. the getJSON method is used as an example to describe the data processing method:

Copy codeThe Code is as follows:
$. GetJSON ("http://user.qzone.qq.com/2227211070", {param: "gaoyusi"}, 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 );
});
});

The above is all the content about json parsing by jQuery. I hope you will like it.

Related Article

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.