是否可以列出我的JSON中的所有设施,但仍然可以访问使用酒店信息?目前只有在我吐出设施时它才有效。
JSON
{
"id": "1",
"hotel_title": "Name of Hotel",
"hotel_description": "10",
"facilities": [
{
"id": "1",
"facility_name": "Foo",
"facility_description": "Bar",
"pivot": {
"hotel_id": "1",
"facility_id": "1"
}
}
]
}
致电JSON
$.getJSON("{{ url('api/facilitieslist')}}", { option: $(this).val() },
function(data) {
$('#facilities').empty();
$.each(data, function(key, value) {
$('#facilities').append('<div><input type="checkbox" value="' + value.id +'" name="facilities[]"> ' + value.name +'</div>');
});
});
答案 0 :(得分:1)
我不认为你提出的样本实际上是有效的(你是在循环数据而不是数据。设备和你的属性名称是不明确的,并且不存在于你提出的JSON对象中),但是,此示例显示如何遍历您的设施并仍然访问您的酒店数据(我认为这是您想要的):
var data = {
"id": "1",
"hotel_title": "Name of Hotel",
"hotel_description": "10",
"facilities": [
{
"id": "1",
"facility_name": "Foo",
"facility_description": "Bar",
"pivot": {
"hotel_id": "1",
"facility_id": "1"
}
}
]
};
$.each(data.facilities, function(key, value) {
console.log('<div>' + value.facility_name + ' - ' + data.hotel_title +'</div>');
});