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請求:
$.ajax({ url: '/path/to/file', type: 'GET', dataType: 'json', data: {param1: 'value1'}, success: function (obj){ //遍歷obj } })
返回的內容在success的函數里面,所有的遍歷操作都是在這里面操作的:
for循環:
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("數據有誤~"); };
for in 循環:
//for in循環 for(x in obj.bkdata){ //x表示是下標,來指定變量,指定的變量可以是數組元素,也可以是對象的屬性。 console.log(obj.bkdata[x]); } //元素 each方法if (obj.status == 1) { $(obj.bkdata).each(function(index,item){ //index指下標 //item指代對應元素內容 //this指代每一個元素對象 //console.log(obj.bkdata[index]); console.log(item); //console.log($(this)); }); }else{ alert("數據有誤~"); }; //jquery each方法$.each( obj.bkdata, function(index,item){ console.log(item); });
jQuery的ajax和遍歷json數組代碼如下所示:
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); }) } });
新聞熱點
疑難解答