Reputation: 3231
Despite reading almost every article on the first page of Google about this, I can't seem to get this to work:
[ { "comment" : "Resources to learn the basics of financial markets and economic concepts",
"id" : "1",
"name" : "Basics of Finance"
},
{ "comment" : "Pictures of colourful artwork to uplift the mood.",
"id" : "2",
"name" : "Colourful Artwork"
}
]
This is my JSON file which I want to parse with jQuery/javascript and use. This is what I'm trying at the moment:
$.ajax({
url: 'yourcurations.php',
data: '',
dataType: 'json',
success: function(data){
$.each($.parseJSON(data), function(i, item) {
alert(item.name);
});
}
});
where 'data' is the JSON pasted above, but it's not working. Can anyone help?
Thanks
Upvotes: 0
Views: 68
Reputation: 119837
The returned JSON data is already parsed because you already stated that it's JSON. And even without stating the return type, jQuery "guesses" the type of the data that's returned and parses it accordingly. Therefore, data
is already an array and you don't need too parse anymore.
Also, use console.log
when debugging. It gives you more detail about the value.
success: function(data){
$.each(data, function(i, item) {
console.log(item.name);
});
}
Upvotes: 2