JQuery's method for Traversing json (recommended), jqueryjson
var obj = {"status":1,"bkmsg":"\u6210\u529f","bkdata":["\u5415\u5c1a\u5fd7","1387580400","\u6dfb\u52a0\u8bb0\u5f55"]}{"status":1,"bkmsg":"\u6210\u529f","bkdata":["\u5415\u5c1a\u5fd7","1387580400","\u6dfb\u52a0\u8bb0\u5f55"]},{"status":1,"bkmsg":"\u6210\u529f","bkdata":["\u5415\u5c1a\u5fd7","1387580400","\u4ec0\u4e48\u4e5f\u6ca1\u6709"]}
Ajax request:
$. Ajax ({url: '/path/to/file', type: 'get', dataType: 'json', data: {param1: 'value1'}, success: function (obj) {// traverse obj }})
The returned content is in the success function. All traversal operations are performed here:
For Loop:
Var obj = {"status": 1, "bkmsg": "\ u6210 \ u529f", "bkdata": ["\ u5415 \ u5c1a \ u5fd7", "1387580400 ", "\ u6dfb \ u52a0 \ u8bb0 \ u5f55"]} // console. log (obj. length); if (obj. status = 1) {for (var I = 0; I <obj. bkdata. length; I ++) {console. log (obj. bkdata [I]) ;};} else {alert ("data error ~ ");};
For in loop:
// For in loop for (x in obj. bkdata) {// x indicates a subscript to specify a variable. The specified variable can be an array element or an object attribute. Console. log (obj. bkdata [x]);} // element each method if (obj. status = 1) {$ (obj. bkdata ). each (function (index, item) {// index indicates the subscript // item indicates the content of the corresponding element. // this indicates each element object // console. log (obj. bkdata [index]); console. log (item); // console. log ($ (this) ;}) ;}else {alert ("data error ~ ") ;}; // Jquery each method $. each (obj. bkdata, function (index, item) {console. log (item );});
The ajax and json array traversal code of jQuery is as follows:
jQuery.ajax({ type: "POST", url: "server.json", dataType:'json', data: "", success: function(msg){ var title = ""; jQuery.each(msg,function(key,value){ alert(value.ec_id+" "+value.ec_title); }) } });