JSON (JavaScript Object notation) is a lightweight data interchange format that is easy to read and write, and is also easy to machine parse and generate, and is widely used today as well as XML. Some people say that when JS meets JSON, Little JJ is born; I say that when JSON encounters XML, there are two more brothers in the world. The recent Easyui video is also full of JSON as a data interaction between the front and back, JSON has two structures, namely, objects and arrays:
1. Object: Object in JS is expressed as "{}" to expand content, data structure is: {Key:value,key:value,...}
2. Array: The array in JS is in the brackets "[]" expanded content, data structure is: ["Java", "JavaScript", "VB",...]
Through the object, the array of 2 structure can be combined into a complex data structure. With a simple understanding of JSON, let's take a look at how jquery parses the JSON string and how it is applied to actual development, listing two ways of parsing:
jquery parses JSON data format:
Here we first give a set of JSON strings, and the string set is as follows:
The code is as follows |
Copy Code |
var data= " { Root [ {name: ' 1 ', Value: ' 0 '}, {name: ' 6101 ', Value: ' Xian City '}, {name: ' 6102 ', Value: ' Tongchuan '}, {name: ' 6103 ', Value: ' Baoji '}, {name: ' 6104 ', Value: ' Xianyang '}, {name: ' 6105 ', Value: ' Weinan '}, {name: ' 6106 ', Value: ' Yanan '}, {name: ' 6107 ', Value: ' Hanzhong '}, {name: ' 6108 ', Value: ' Yulin '}, {name: ' 6109 ', Value: ' Ankang '}, {name: ' 6110 ', Value: ' Shangluo '} ] }"; |
This 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, as illustrated in the following example:
Example
The code is as follows |
Copy Code |
var dataobj=eval ("(" +data+ ")");//Convert to JSON object Alert (dataObj.root.length)//output The number of child objects of root $.each (Dataobj.root,fucntion (Idx,item) { if (idx==0) { return true; } Output the name and value of each root child object Alert ("Name: +item.name+", Value: "+item.value"); }) |
Note: For the general JS generation JSON object, only need to replace the $.each () method with the For statement, the other unchanged.
Example
Introduction method and Simple example
Introducing an example:
code is as follows |
copy code |
<script Type= "Text/javascript" src= "/js/jquery-1.2.6.min.js" ></script><script type= "Text/javascript" Src= "/js/jquery.json.js" ></script> Use instance: //define a JS object var thing = {plugin: ' Jquery-json ', version : 2.3};//JS object into JSON string ' {' plugin ': ' Jquery-json ', ' Version ': 2.3} ' var encoded = $.tojson (thing);//parse string as JS object " Jquery-json "var name = $.evaljson (encoded). Plugin;//2.3var Version = $.evaljson (encoded). version; |
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:
The code is as follows |
Copy Code |
$.getjson ("http://www.111cn.net/", {param: "Gaoyusi"},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 take a look at the syntax:
The code is as follows |
Copy Code |
Data = $.parsejson (string); |
See how it is applied to the actual development:
The code is as follows |
Copy Code |
Jquery.ajax ({ Url:dataurl, Success:function (Results) { var Parsedjson = Jquery.parsejson (results); alert (parsedjson.name); } }); |
As you can see, here, I've used Parsejson to parse the JSON string into objects, so here you can't get the value directly, you need to use the parsed value of the object. However, JSON is fairly simple and easy to read, and once you get used to curly braces and parentheses, you'll be deeply aware of the benefits that JSON brings!