本文介绍了JavaScript的遍历JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为它是一件容易的事......我打算使用下面的JSON构建路由器的对象。我把的console.log,所以我可以有一个破发点光斑,所以我可以尝试找出如何从Chrome控制台访问的对象属性。它从未进入for循环,虽然。

I always think it's going to be easy... I plan to use the json below to build router objects. 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. It never goes into the for loop though.

的主要问题是如何正确地转成JSON对象以及如何访问它的属性。

The main question is how to properly turn the JSON into objects and how to access it's properties.

<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"
        ]
    }
}

}

推荐答案

JSON是在JavaScript原生提供,你遍历它像你遍历任何对象或数组。

JSON is natively available in JavaScript, you traverse it like you would traverse any object or array.

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 (var key in json) {
    console.log(key);   // _id, DSC21, DCS22 etc..
    console.log(json[key]);    // { "$oid": "" }, { "Router": ".." } etc.
}

这确实离开的hasOwnProperty 的问题,但它不应该是一个问题,如果你只是读JSON数据。

This does leave the hasOwnProperty issue, but it shouldn't be a problem if you're just reading JSON data.

这篇关于JavaScript的遍历JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 05:54