时间:2021-05-26
本文实例总结了JSON遍历方式。分享给大家供大家参考,具体如下:
第一种:使用for循环
js代码:
function CyclingJson1() { var testJson = '[{ "name": "小强", "age": 16 },{"name":"小明","age":17}]'; testJson = eval("(" + testJson + ")"); for (var i = 0; i < testJson.length; i++) { alert(testJson[i].name); } }第二种:使用关键字in
js代码:
function CyclingJson2() { var testJson = '[{ "name": "小强", "age": 16 },{"name":"小明","age":17}]'; testJson = eval("(" + testJson + ")"); for (var i in testJson) { alert(testJson[i].name); }}第三种;使用jquery.each()函数
function CyclingJson3() { var testJson = '[{ "name": "小强", "age": 16 },{"name":"小明","age":17}]'; testJson = eval("(" + testJson + ")"); $.each(testJson, function (i, n) { alert(i); //i为索引值 alert(n.name); //n为遍历的值 });}综合实例:
<script language="javascript" type="text/javascript">var array = { "a": "abc", "b": [1, 2, 3, 4, 5, 6], "c": 3, "d": { "name": "james", "age": 28},"e":null,"f":true };var arrayObj = { "a": { "name": "kobe", "age": 34 }, "b": { "name": "lofo", "age": 28} };//遍历array方式1for (var x in array) { if (typeof array[x] == 'object' && array[x] != null ) { for (var y in array[x]) { alert("key = " + y + " value = " + array[x][y]); } } else { alert("key = " + x + " value = " + array[x]); // 非array object }}//遍历array方式2$.each(array, function(k, v) { var a = typeof (v); //数组与对象为object类型,其他分别是string与number类型 //此时的k代表a、b、c、d,v代表对应的值 if (typeof (v) == "object") { //获取数组与对象后,再去遍历这个数组与对象 $.each(v, function(k1, v1) { window.alert("key = " + k1 + " value=" + v1); }); } else { window.alert("key = " + k + " value=" + v); }});//遍历arrayObj方式1for (var x in arrayObj) { for (var key in arrayObj[x]) { window.alert("key=" + key + " value=" + arrayObj[x][key]); }}//遍历arrayObj方式2$.each(arrayObj, function(key, value) { $.each(value, function(k, v) { window.alert("key=" + k + " value=" + v); });});//遍历arrayObj方式3$.each(arrayObj, function(key, value) { window.alert("key=" + key + " valueName=" + value.name + " valueAge=" + value.age);});</script><script language="javascript" type="text/javascript">var UserList = [ { "UserID": 11, "Name": { "FirstName": "Truly", "LastName": "Zhu" }, "Email": "" }, { "UserID": 12, "Name": { "FirstName": "Jeffrey", "LastName": "Richter" }, "Email": "" }, { "UserID": 13, "Name": { "FirstName": "Scott", "LastName": "Gu" }, "Email": "" }]$.each(UserList, function(k, v) { $.each(v, function(k1, v1) { if (typeof (v1) == "object") { $.each(v1, function(k2, v2) { window.alert("key=" + k2 + " value=" + v2); }); } else { window.alert("key=" + k1 + " value=" + v1); } });});</script><script language="javascript" type="text/javascript">var userArray = [ { "UserID": 11, "Name": { "FirstName": "Truly", "LastName": "Zhu" }, "Email": "" }, { "UserID": 12, "Name": { "FirstName": "Jeffrey", "LastName": "Richter" }, "Email": "" }, { "UserID": 13, "Name": { "FirstName": "Scott", "LastName": "Gu" }, "Email": "" }];for (var i in userArray) { for (var k in userArray[i]) { if (typeof userArray[i][k] == "object" && userArray[i][k] != null) { for (var j in userArray[i][k]) { window.alert("key=" + j + "--value=" + userArray[i][k][j]); } } else { window.alert("key=" + k + "--value=" + userArray[i][k]); } }}</script>希望本文所述对大家JavaScript程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了JS遍历json和JQuery遍历json操作。分享给大家供大家参考,具体如下:json遍历demodatavarjson={"test1":"d
本文实例讲述了JS遍历JSON数组及获取JSON数组长度操作。分享给大家供大家参考,具体如下:遍历JSON数组:varquestions=data.data.q
本文实例总结了Java实现Json字符串与Object对象相互转换的方式。分享给大家供大家参考,具体如下:Json-Lib、Org.Json、Jackson、G
本文实例讲述了JS实现键值对遍历json数组功能。分享给大家供大家参考,具体如下:www.jb51.netjs键值对遍历json数组vardata=[];var
本文实例讲述了Vue指令v-for遍历输出JavaScript数组及json对象的常见方式。分享给大家供大家参考,具体如下:定义数据:newVue({el:"#