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); } }); |