javascript遍历json对象

时间:2012-04-21 18:25:45

标签: javascript ajax json object

我一直认为这很容易......我打算用下面的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"
        ]
    }
}

}

3 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分: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数据则不应该出现问题。

答案 2 :(得分: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...
   }
}