jquery Parse JSON Data sample code

Source: Internet
Author: User
Tags eval json

  As the transport object for the jquery asynchronous request, the result returned by the jquery request is the JSON object, which takes into account the form of a string that the server returns in JSON form

Here you'll find Json.js code, followed by Formutil.js code and md5.js    parsing JSON data using jquery as a transmission object for jquery asynchronous requests, The result of the jquery request return is the JSON object, which takes into account the form of a string returned in JSON by the server, which is similar to the JSON object encapsulated by plug-ins such as Jsonobject, which is no longer explained.   Here we first give a set of JSON strings, the string set below:  code as follows:  code as follows: Var data= "  {  root:  [  {name: ' 1 ', Value: ' 0 '} ,  {name: ' 6101 ', Value: ' Beijing '},  {name: ' 6102 ', Value: ' Tianjin '},  {name: ' 6103 ', Value: ' Shanghai '},  {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 '} ]  } ";    Here is based on the data type--json object and string that jquery asynchronously obtains, and describes the result processing methods obtained in two ways respectively.   1. For the JSON string returned by the server, if the jquery asynchronous request does not have a type description, or is accepted as a string, it is necessary to do an object-by-case, either in a cumbersome way, or to put the string in eval () once. This is also a good way to get JSON objects in a normal javascipt way, with the following examples:    code as follows: Var dataobj=eval ("(" +data+) ");//Convert to JSON object   alert ( dataObj.root.length)//output root number of child objects   $.each (Dataobj.root,fucntion (Idx,item) {  if (idx==0) {  return true; } //Output the name and value of each root child   alert ("Name: +item.name+", Value: "+item.value); }"     NOTE: To generate a JSON object for a generic JS, simply replace the $.each () method with the For statement, and the other unchanged.   2. For the JSON string returned by the server, the eval () method is not required if the jquery asynchronous request sets the type (typically this configuration property) to "JSON" or uses the $.getjson () method to get the server back. Because the result is already a JSON object, just call the object directly, and here take the $.getjson method as an example to illustrate the data processing method:    code as follows: $.getjson ("http://www.phpzixue.cn/" , {param: "Gaoyusi"},function (data) { //The data returned here is already a JSON object  //The following other operations with 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); }); });    Here it is particularly noteworthy that the eval () method in mode 1 is dynamically executing a string (possibly a JS script). This can easily cause a system security problem. So you can use a few Third-party client script libraries that circumvent eval (), such as JSON in JavaScript, which provides a script library of no more than 3k.    

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.