How can I access objects that sit within an array, which is itself inside a JSON object?
JSON Structure:
{
"name":"animals",
"type":"farmyard",
"version": "1.0",
"items": [{
{
"name": "pig",
"description": "pink, round"
},
{
"name": "chicken",
"description": "small, yellow"
}
}]
}
And here is the JS so far...
$.getJSON( "https://_LINK_TO_JSON.json", function( data ) {
var farm = [];
var animals = [];
$.each( data, function( key, val ) {
farm.push(key, val);
var animals = farm[3];
console.dir(animals);
});
console.dir(animals);
});
I've tried to use farm.items to target the array, but that didn't work so I've used the index number instead.
(Naturally, using farm.items[1].name to target the first name didn't work either.)
Is the reason I can't just use dot notation something to do with the fact that JSON keys and valuesare within quote marks? (I can't actually edit the JSON feed as it's external).
How can I simply target the nested array and grab items I want and their properties?
{ }in the "items" array is probably a typo. If so, then iterating throughdata.itemswould give you access to the elements of the array.