json对象循环遍历

JSON可以有两种格式
一种是对象格式的:

{“name”:“Tom”,“sex”:“male”,“age”:20} //JSON的对象格式的字符串

另一种是数组对象

[{“name”:“Lily”,“sex”:“female”,“age”:20}] //数据对象格式

var  json1 = {'name':'张三','sex':'男','age':18}
  for(var key in json1){
    console.log(key) //naem sex age
    console.log(json1[key]) //张三 男 18
  }
var json2 = [{'name':'张三','sex':'男','age':18},{"name":"Tom","sex":"male","age":20},{"name":"Lily","sex":"female","age":20}]

for (var i = 0; i < json2.length; i++) {
  for(let key in json2[i]){
    console.log(json2[i][key])
  }
}

你可能感兴趣的:(JavaScript,javascript)