I am trying to loop through a simple JSON array and display the contents with jQuery. My JSON data is:
{
"cards":[
{
"title":"cat",
"spanishWord":"gato"
},
{
"title":"dog",
"spanishWord":"perro"
}
]
}
Here is the jQuery I am using:
var jqxhr = $.getJSON("http://www.myurl.com/cards.js", function (data) {
$.each(data.cards, function (i, item) {
$(".list").append("<li id='" + cards[i].title + "'>" + cards[i].title + cards[i].spanishWord + "</li>");
});
});
I am pretty certain the problem is in my each statement but I can't figure out what is wrong.
asked Oct 6, 2013 at 16:32
user715564
1,7003 gold badges26 silver badges62 bronze badges
-
item.title will be cleanercodeandcloud– codeandcloud2013年10月06日 16:37:42 +00:00Commented Oct 6, 2013 at 16:37
1 Answer 1
The problem is inside the loop, where you use cards[i] instead of data.cards[i].
You could also use item instead of data.cards[i].
answered Oct 6, 2013 at 16:34
Guffa
703k112 gold badges760 silver badges1k bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
user715564
Perfect, thanks! I'll accept your answer when the time limit is up.
Michael Geary
Be sure to use
item instead of data.cards[i]. Much cleaner!lang-js