1

我一直认为这会很容易......我打算使用下面的json来构建路由器对象。我放了一个console.log,这样我就可以有一个断点点,这样我就可以尝试弄清楚如何从chrome控制台访问对象属性。但它永远不会进入 for 循环。

主要问题是如何正确地将 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"
        ]
    }
}

}

4

3 回答 3

4

变量json已经是一个对象,但它不是一个数组,所以典型的 for 循环是不够的。由于json.length未定义,i<json.length因此在第一次迭代时失败并且您跳过循环。

for (var key in json) {
    // key is your DSCxxx
    // json[key] is the corresponding object
}
于 2012-04-21T18:31:10.360 回答
3

JSON 在 JavaScript 中是原生可用的,您可以像遍历任何对象或数组一样遍历它。

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

如果您不知道属性的名称并想遍历它们,请使用以下for .. in构造:

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

这确实会留下hasOwnProperty问题,但如果您只是读取 JSON 数据,这应该不是问题。

于 2012-04-21T18:33:34.573 回答
1

也许你想知道如何迭代你的对象?

这将是如何做到这一点:

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...
   }
}
于 2012-04-21T18:32:40.907 回答