i have this simple jquery script to loop through JSON array
the script is not working at it all and never give output.
im sure that the JSON array is valid but i don't know why Jquery not parsing it .
$(document).ready(function(){
var cost = [{"gold":"100","iron":"80","wood":"120","food":"70"},{"gold":"80","iron":"60","wood":"90","food":"35"}];
var costarr = $.parseJSON(cost);
$.each(costarr, function(i, item) {
alert(item.gold);
}
});
i have this simple jquery script to loop through JSON array
the script is not working at it all and never give output.
im sure that the JSON array is valid but i don't know why Jquery not parsing it .
$(document).ready(function(){
var cost = [{"gold":"100","iron":"80","wood":"120","food":"70"},{"gold":"80","iron":"60","wood":"90","food":"35"}];
var costarr = $.parseJSON(cost);
$.each(costarr, function(i, item) {
alert(item.gold);
}
});
Share
Improve this question
asked Apr 13, 2013 at 23:07
Dr.NeoDr.Neo
1,2804 gold badges17 silver badges29 bronze badges
1
- 2 if you want to play with javascript at least use a browser console to inspect errors thrown...pretty easy to locate syntax errors you have...and it takes all of 3 seconds to do it! – charlietfl Commented Apr 13, 2013 at 23:14
2 Answers
Reset to default 8You don't need to parse it, it's already an array. And your each
lacks a closing )
$.each(cost, function(i, item) {
alert(item.gold);
}); //<-- lacking ")"
You have a syntax error.
$.each(costarr, function(i, item) {
alert(item.gold);
}
is missing the ending ');'
which is why nothing is being alerted in your fiddle.