[jQuery] How to loop through JSON data
If I have JSON data like this:
{ "9": { "title": "Event 1", "style": "rock" }, "11": { "title":
"Event 2", "style": "pop" }, "15": { "title": "Event 3", "style":
"house" }, "16": { "title": "Event 4", "style": "metal"} }
How can I loop through each item grabbing each key/value pair for
outputting? Is this possible with $.each?
I tried the following:
var event = '';
$.each(results, function(title, style) {
event += '<div>' + title + ' ' + style + '<\/div>';
});
$('#events').html(event);
But it returned
9 [object Object]
11 [object Object]
15 [object Object]
16 [object Object]
I'm guessing my syntax is wrong or I am misusing the function. Any
help appreciated.