我有以下JS来循环访问JSON数据,但是当我得到未定义时,因为information
是数组中的一个对象而我不确定如何更正我的代码。
还有我如何将输出包装成单独的div?
$.getJSON('js/test.json', function(data) {
var importantInfo="<ul>";
for (var i in data.hotelListing) {
importantInfo+="<li>" + data.hotelListing[i].information.leaving + "</li>" + "<li>" + data.hotelListing[i].information.departureAirport + "</li>";
}
importantInfo+="</ul>";
$(importantInfo).appendTo("#hotelInfo");
});
{ "hotelListing" : [
{ "country" : "Austria",
"information" : [
{ "airline" : "Thomson Airways",
"leaving" : "21 Feb 2015 14:30"
} ],
"price" : "£499"
},
{ "country" : "Austria",
"information" : [
{ "airline" : "Thomson Airways",
"leaving" : "21 Feb 2015 14:30"
} ],
"price" : "£549"
}
] }