js遍历json
2021-05-16 10:29
标签:value .com icon data- tis post bsp lin blank 遍历json对象: 无规律: 有规律: 也可以用这个: 遍历json对象 js遍历json 标签:value .com icon data- tis post bsp lin blank 原文地址:http://www.cnblogs.com/kongxc/p/7747478.htmljs遍历json的key和value
原生js遍历json对象
packJson = [
{"name":"nikita", "password":"1111"},
{"name":"tony", "password":"2222"}
];
for(var p in packJson){//遍历json数组时,这么写p为索引,0,1
alert(packJson[p].name + " " + packJson[p].password);
}
for(var i = 0; i
myJson = {"name":"caibaojian", "password":"1111"};
for(var p in myJson){//遍历json对象的每个key/value对,p为key
alert(p + " " + myJson[p]);
}
有如下 json对象:
var obj ={"name":"冯娟","password":"123456","department":"技术部","sex":"女","old":30};
遍历方法:
for(var p in obj){
str = str+obj[p]+’,‘;
return str;
}