jQuery遍历json的方法(推荐)
2019/6/27 22:21:43
本文主要是介绍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); }) } });
这篇关于jQuery遍历json的方法(推荐)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-04package.json 文件位置在哪?-icode9专业技术文章分享
- 2024-10-01Craco.js学习:从入门到实践指南
- 2024-10-01Create-React-App学习:入门与实践指南
- 2024-10-01CSS-in-JS学习:从入门到实践指南
- 2024-09-30JSX语法学习:从入门到初步掌握
- 2024-09-30Mock.js学习:入门教程与实战演练
- 2024-09-30React Hooks学习:从入门到实践
- 2024-09-30受控组件学习:React中的基础入门教程
- 2024-09-29JS定时器教程:初学者必看指南
- 2024-09-29JS对象教程:初学者的全面指南