Ok, a little new to JSON format..
I have the following JSON string returned from an AJAX call, which firebug actually displays in a tree quite nicely.. however I can’t seem to be able to work out how to loop through the content…
{"data":{"item":[{"@id":"7","fromMemberID":"7","FromMember":"david","notificationsType":"event","notificationsDesc":"A new event (Test Event Thursday, 16 September 2010) has been created.","notificationsDate":"16 Sep 2010","notificationsTime":"00:02:18"},{"@id":"8","fromMemberID":"7","FromMember":"david","notificationsType":"event","notificationsDesc":"A new event (Test Event Thursday, 16 September 2010) has been created.","notificationsDate":"16 Sep 2010","notificationsTime":"08:26:24"}]}}
I have tried to say get a count of items.. alert(data.item.length);
or a loop:
for(i=0; i<data.item.length; i++)
{
alert(data.item[i].FromMember);
}
obviously missing something fundemental…
Any ideas??
You were very close… “data” is actually a key in your JSON, so you have to refer to your JSON variable to access “data”…. so you want
JSON.data.item[i].FromMemberHere is some full working code:
jsFiddle