I've been working with an API and managed to successfully load a JSON array of objects in my browser. Here's a snippet of what it looks like:
0:
source: {id: null, name: "Protothema.uk"}
author: "james bond"
title: " A TITLE"
description: "A DESCRIPTION"
__proto__: Object
Here is the code I'm using:
$.getJSON("http://newsapi.org/v2/top-headlines?country=uk&category=health&apiKey=MYAPIKEY", function(data){
//console.log(data);
$.each(data,function(index,value){
console.log(value);
console.log(value[0]);
console.log(value[0].title)//Cannot read property 'title' of undefined
});
});
When trying to print the entire index with console.log(value[0]);
, I am able to see all the objects within index 0.
However, when attempting to print a specific value for a key like console.log(value[0].title)
, I encounter an error stating Cannot read property 'title' of undefined.
I've been stuck on this problem for hours. Can anyone point out where I might be going wrong?