简体   繁体   English

javascript遍历json对象

[英]javascript traverse json object

I always think it's going to be easy... I plan to use the json below to build router objects. 我一直认为这很容易...我计划使用下面的json构建路由器对象。 I put a console.log and so I could have a break point spot so I could try to figure out how to access the the object properties from the chrome console. 我放置了console.log,所以我可以有一个断点,以便尝试找出如何从chrome控制台访问对象属性。 It never goes into the for loop though. 但是它永远不会进入for循环。

The main question is how to properly turn the JSON into objects and how to access it's properties. 主要问题是如何正确地将JSON转换为对象以及如何访问其属性。

<script type="text/javascript">
    $(document).ready(function(){

        $.getJSON('JSON/data.json', function(json) {

            for (var i=0;i<json.length;i++){
                console.log("in for loop");
            }

        });
    });

</script>





{
"_id": {
    "$oid": "4f91f2c9e4b0d0a881cf86c4"
},
"DSC21": {
    "Router": {
        "online": [
            "1",
            "1",
            "1",
            "1",
            "1",
            "1",
            "1",
            "1",
            "1",
            "1"
        ],
        "bytes": [
            "59.5721304971465",
            "17014.1911069063",
            "14858.8518936735",
            "6875.20981475265",
            "15157.6891384625",
            "6363.47544785913",
            "29446.2111270486",
            "11517.9296243171",
            "27077.9747917112",
            "19867.79381695"
        ]
    }
},
"DSC22": {
    "Router": {
        "online": [
            "1",
            "1",
            "1",
            "1",
            "1",
            "1",
            "1",
            "1",
            "1",
            "1"
        ],
        "bytes": [
            "59.5721304971465",
            "17014.1911069063",
            "14858.8518936735",
            "6875.20981475265",
            "15157.6891384625",
            "6363.47544785913",
            "29446.2111270486",
            "11517.9296243171",
            "27077.9747917112",
            "19867.79381695"
        ]
    }
},
"DSC23": {
    "Router": {
        "online": [
            "1",
            "1",
            "1",
            "1",
            "1",
            "1",
            "1",
            "1",
            "1",
            "1"
        ],
        "bytes": [
            "59.5721304971465",
            "17014.1911069063",
            "14858.8518936735",
            "6875.20981475265",
            "15157.6891384625",
            "6363.47544785913",
            "29446.2111270486",
            "11517.9296243171",
            "27077.9747917112",
            "19867.79381695"
        ]
    }
},
"DSC24": {
    "Router": {
        "online": [
            "1",
            "1",
            "1",
            "1",
            "1",
            "1",
            "1",
            "1",
            "1",
            "1"
        ],
        "bytes": [
            "59.5721304971465",
            "17014.1911069063",
            "14858.8518936735",
            "6875.20981475265",
            "15157.6891384625",
            "6363.47544785913",
            "29446.2111270486",
            "11517.9296243171",
            "27077.9747917112",
            "19867.79381695"
        ]
    }
}

} }

The variable json is already an object, but it is not an array, so a typical for-loop is insufficient. 变量json已经是一个对象,但它不是数组,因此典型的for循环不足。 Since json.length is undefined, i<json.length fails on the first iteration and you skip over the loop. 由于json.length是未定义的,因此i<json.length在第一次迭代时失败,您将跳过循环。

for (var key in json) {
    // key is your DSCxxx
    // json[key] is the corresponding object
}

JSON is natively available in JavaScript, you traverse it like you would traverse any object or array. JSON在JavaScript中是本机可用的,遍历它就像遍历任何对象或数组一样。

json["DSC21"]["Router"]["online"][0];    // 1
json.DSC21.Router.online[0];    // equivalent
json.DSC21.Router.online.0;    // INCORRECT

If you don't know the names of the properties and want to loop through them use the for .. in construction: 如果您不知道属性名称,并且想遍历它们for .. in构造中使用for .. in

for (var key in json) {
    console.log(key);   // _id, DSC21, DCS22 etc..
    console.log(json[key]);    // { "$oid": "" }, { "Router": ".." } etc.
}

This does leave the hasOwnProperty issue, but it shouldn't be a problem if you're just reading JSON data. 这确实留下了hasOwnProperty问题,但是如果您只是读取JSON数据,那应该不成问题。

maybe you want to know how to iterate your objects? 也许您想知道如何迭代对象?

here would be how to do that: 这将是如何做到的:

for( var key in json ){
   if( key != '_id'){
      var router = json[key].Router;
      for( var i = 0; i < router.online.length; i++ ){
        console.log(i + ' is online: ', router.online[i]==1?'true':'false');
      }
      etc...
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM