javascript - Getting value from JSON when it is an object in an array in an object in an array - if that makes sense? -
i have been working on displaying values json feed , - with great deal of thanks on many previous posts have read here - can write (display in browser) values of objects in main array values of 'name' objects 'nested' in array > object > array > object > proprty:value structure (as below). wondering if might me kind enough me find way interate , display level - feel have run out of ideas on @ moment :-(
very thankful ideas. have been using standard javascript not jquery should add.
var foo=[ { "subjects": [ "a1", "sb2" ], "title": "box world", "first_pub": "2013-12", "characters": [ { "name": { "given": "maxwell", "honourific": "", "family": "smart" }, "id": "ms34" }, { "name": { "given": "samantha", "honourific": "", "family": "stevens" }, "id": "ss81" } // end creators object 2 ], "publisher": "galactic publishing" }, { "subjects": [ "a133", "pb82" ], "title": "octonautica", "first_pub": "2010", "characters": [ { "name": { "given": "peso", "honourific": "doctor", "family": "" }, "id": "ms34" }, { "name": { "given": "barnacle", "honourific": "captain", "family": "" }, "id": "ss82" } ], "publisher": "neptune house" } ] var obj_set = foo ; (var key in obj_set) { document.write("\<h3\> publication " + key + "\<\/h3\>" ); var obj = obj_set[key]; document.write("\<ol\>"); (var prop in obj) { document.write("\<li\>" + prop + " = " + obj[prop] + "\<\/li\>" ); } document.write("\<\/ol\>"); } //end main key loop
try instead:
var i, j; // iterate on main array foo (i=0; i<foo.length; i++) { // iterate on characters array for(j=0; j<foo[i].characters.length; j++) { // each item contains object "name" var name = foo[i].characters[j].name; console.log(name.honorific + ' ' + name.given + ' ' + name.family); } }
Comments
Post a Comment