jQuery遍历json的方法(推荐)
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); }) } });
相关推荐
EdwardSiCong 2020-11-23
85477104 2020-11-17
hhanbj 2020-11-17
81427005 2020-11-11
seoppt 2020-09-13
honeyth 2020-09-13
WRITEFORSHARE 2020-09-13
84483065 2020-09-11
momode 2020-09-11
85477104 2020-08-15
83510998 2020-08-08
82550495 2020-08-03
tthappyer 2020-08-03
84901334 2020-07-28
tthappyer 2020-07-25
TONIYH 2020-07-22
tztzyzyz 2020-07-20
83510998 2020-07-18
81463166 2020-07-17