javascript - How to access nested JSON object key and values -


i trying access key , values of nested array this:

var obj = $.getjson("mydata.json", function() {     console.log( "load success" ); });  object.keys(obj[2].type).foreach(function(key) {     console.log(key, obj[key]); }); 

but error.

here's structure of json file:

{ "nodes": [     {         "nd": "nd1",         "cat": "cat1"     } ], "links": [     {         "id": 100     } ], "types": [             {                 "type": "one",                 "image": "image001"             },             {                 "type": "two",                 "image": "image002"             },             {                 "type": "three",                 "image": "image003"             }         ]    

}

my goal list of values of:

one 2 three

something that?

you have firstly acces object.types attribute, iterate on retrieve every type.

var object = {    "nodes": [{      "nd": "nd1",      "cat": "cat1"    }],    "links": [{      "id": 100    }],    "types": [{      "type": "one",      "image": "image001"    }, {      "type": "two",      "image": "image002"    }, {      "type": "three",      "image": "image003"    }]  };      function parsejson(object){      object.types.foreach(function(key) {        console.log(key.type);      });  }    parsejson(object);

--- update anwer question in comment ---

you enclose code in function, , call when load json:

$.getjson("mydata.json", function(data) {     parsejson(data); }); 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -