简体   繁体   中英

javascript traverse json object

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.

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

}

The variable json is already an object, but it is not an array, so a typical for-loop is insufficient. Since json.length is undefined, i<json.length fails on the first iteration and you skip over the loop.

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["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.
}

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

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...
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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