I am trying to output some JSON objects in the console but it doesn't seem to work. This JSON comes from a mini API that I have created using PHP.
My JSON looks like this:
{
TotalResults: 2,
Results: [
{
UserName: "Sarah",
ModifDate: "2014-12-01T08:03:40.000+00:00",
ID: "54321",
Title: "Brilliant",
RText: "This is a brilliant product"
},
{
UserName: "Joe",
LastModificationTime: "2014-11-01T08:03:40.000+00:00",
Id: "12345",
Title: "Excellent",
RText: "This is a great console,"
}
]
}
My Javascript:
$.ajax({
url:'http://myapi.com?api=1&test=test',
dataType: 'json',
type: 'get',
cache: false,
success:function(data){
for (var i in data) {
var count = data[i].TotalResults;
var name = data[i].Results.UserName;
var text = data[i].Results.RText;
console.log(name);
console.log(text);
}
}
});
However, nothing is being returned and I get the following in the console log:
Uncaught TypeError: Cannot read property 'TotalResults' of undefined
Do you guys have an easier way to read JSON data in an array?
I eventually want to output this data in the DOM as HTML.
data... sometimes it doesn't look exactly like we expect. This might give you the information to move forward.console.log(data);and what doesconsole.log(text);returns?