How to parse JSON data using jquery

Source: Internet
Author: User

The JSON data parsing method using jquery is used as the transmission object of jquery asynchronous requests. The result returned by the jquery request is a json object, which is considered in the form of a JSON string returned by the server, JSON objects encapsulated by plug-ins such as JSONObject are similar in that they are not described here. Here we first provide a JSON string set,

The JSON data parsing method using jquery is used as the transmission object of jquery asynchronous requests. The result returned by the jquery request is a json object, which is considered in the form of a JSON string returned by the server, JSON objects encapsulated by plug-ins such as JSONObject are similar in that they are not described here.

Here we first provide a JSON string set, which is as follows:

The Code is as follows:

 
 
  1. Var data ="
  2. {
  3. Root:
  4. [
  5. {Name:'1', Value:'0'},
  6. {Name:'123', Value:'Beijing'},
  7. {Name:'123', Value:'Tianjin'},
  8. {Name:'123', Value:'Shanghai'},
  9. {Name:'123', Value:'Chongqing'},
  10. {Name:'123', Value:'Weinan City'},
  11. {Name:'123', Value:'Yan'an'},
  12. {Name:'123', Value:'Hanzhong City'},
  13. {Name:'123', Value:'Yulin'},
  14. {Name:'123', Value:'Ankang City'},
  15. {Name:'123', Value:'Shangluo City'}
  16. ]
  17. }";

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:

 
 
  1. Var dataObj = eval ("("+ Data +")");// Convert to a json object 
  2. Alert (dataObj. root. length );// Output the number of root sub-objects 
  3. $. Each (dataObj. root, fucntion (idx, item ){
  4. If(Idx =0){
  5. Return True;
  6. }
  7. // Output the name and value of each root sub-Object 
  8. Alert ("Name :"+ Item. name +", Value :"+ Item. value );
  9. })

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:

 
 
  1. $. GetJSON ("Http://www.phpzixue.cn /", {Param:"Gaoyusi"}, Function (data ){
  2. // The returned data is already a json object. 
  3. // The following operations are the same as the first case 
  4. $. Each (data. root, function (idx, item ){
  5. If(Idx =0){
  6. Return True;// Same as countinue, return false with break 
  7. }
  8. Alert ("Name :"+ Item. name +", Value :"+ Item. value );
  9. });
  10. });

Note that the eval () method in method 1 dynamically executes the strings (which may be JavaScript scripts), which can easily cause system security problems. Therefore, some third-party client script libraries that circumvent eval () can be used. For example, JSON in JavaScript provides a script library of no more than 3 K.

  1. JQuery calls the WCF Service to pass JSON objects
  2. Several common methods required for learning jQuery
  3. Construct an ASP. NET Website using XML + XSLT + CSS + JQuery
  4. Use jQuery and PHP to build an Ajax-Driven Web Page
  5. Notes for calling WCF using jQuery

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.