for in 遍历json

Posted 暗惧者

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了for in 遍历json相关的知识,希望对你有一定的参考价值。

 

 

获取对象属性的语法:

  1.obj.attr

  2.obj["attr"]

for in 遍历json的时候,

  1.for(attr in json)中的attr获取到的是json中属性的名字,是个字符串

  2.json[attr]获取到的是属性的值,该属性的值的数据类型  与  json中该属性的值的数据类型  一致。

<!DOCTYLE html>
<html>
<head>
    <meta charset="uft-8" />
</head>
<body>
</body>
</html>
<script>
    var json1 = {name :‘jack‘,age : 25,gender : ‘male‘ }
    for (attr in json1) {
        console.log(attr);//name,age,gender
        console.log(json1[attr]);//jack,25,male
        console.log(typeof json1[attr]);
    }
</script>

 

以上是关于for in 遍历json的主要内容,如果未能解决你的问题,请参考以下文章