本文實例總結了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方式1 for (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方式1 for (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程序設計有所幫助。