jquery遍历json的几种方法
for循环:
1 <script> 2 var obj = { 3 "status":1, 4 "bkmsg":"\u6210\u529f", 5 "bkdata":["\u5415\u5c1a\u5fd7","1387580400","\u6dfb\u52a0\u8bb0\u5f55"] 6 } 7 // console.log(obj.length); 8 if (obj.status == 1) { 9 for (var i = 0; i < obj.bkdata.length; i++) { 10 console.log(obj.bkdata[i]); 11 }; 12 }else{ 13 alert("数据有误~"); 14 }; 15 </script>
for in 循环:
1 <script> 2 var obj = { 3 "status":1, 4 "bkmsg":"\u6210\u529f", 5 "bkdata":["\u5415\u5c1a\u5fd7","1387580400","\u6dfb\u52a0\u8bb0\u5f55"] 6 } 7 //for in循环 8 for(x in obj.bkdata){ 9 //x表示是下标,来指定变量,指定的变量可以是数组元素,也可以是对象的属性。 10 console.log(obj.bkdata[x]); 11 } 12 </script>
元素 each方法
1 <script> 2 var obj = { 3 "status":1, 4 "bkmsg":"\u6210\u529f", 5 "bkdata":["\u5415\u5c1a\u5fd7","1387580400","\u6dfb\u52a0\u8bb0\u5f55"] 6 } 7 8 if (obj.status == 1) { 9 $(obj.bkdata).each(function(index,item){ 10 //index指下标 11 //item指代对应元素内容 12 //this指代每一个元素对象 13 //console.log(obj.bkdata[index]); 14 console.log(item); 15 //console.log($(this)); 16 }); 17 }else{ 18 alert("数据有误~"); 19 }; 20 </script>
jquery each方法
1 <script> 2 var obj = { 3 "status":1, 4 "bkmsg":"\u6210\u529f", 5 "bkdata":["\u5415\u5c1a\u5fd7","1387580400","\u6dfb\u52a0\u8bb0\u5f55"] 6 } 7 8 $.each( obj.bkdata, function(index,item){ 9 console.log(item); 10 }); 11 </script>