Reputation: 295
I'm trying to display JSON array-data using jQuery. I'm using other examples that I found on internet but I keep getting errors and problems.
When I try confirm(val.image); I get a pop-up: "unidentified".
jQuery:
$(document).ready(function(){
$.getJSON( "images.json", function( data ) {
$.each( data, function( key, val ) {
confirm(val.image);
});
});
});
images.json
{
"images": [
{
"name": "Image 1",
"image": "images/image1.jpg"
},
{
"name": "Image 2",
"image": "images/image2.jpg"
}
]
}
(I'm using wamp/localhost).
Upvotes: 0
Views: 562
Reputation: 2425
Assuming you are able to get the JSON properly
You should use data.images instead of data (data is an object where data.images is the array to loop through)
$.each(data.images, function(index, value) {
alert(value.image);
});
Upvotes: 1
Reputation: 24648
Try this:
$.each( data.images, function( i, val ) {
alert(val.image);
alert(val.name);
});
Upvotes: 1