我无法使用 jquery 的 get JSON 函数解析 JSOn。请帮忙。这是html代码:
function getList()
{
$.getJSON("http://localhost/webservice/list.php", function(data){
for (var i=0, len=data.length; i < len; i++)
{
alert(data[i]);
}
});
这是设置为 header('Content-type: application/json') 的 list.php 文件
{
"promos":[
{"promo":{"id":"1","name":"MADINAT JUMEIRAH AL QASR","image":"","stars":"","highlights":""}},
{"promo":{"id":"2","name":"MADINAT JUMEIRAH DAR AL MASYAF","image":"","stars":"","highlights":""}},
{"promo":{"id":"3","name":"ATLANTIS THE PALM","image":"","stars":"","highlights":""}},
{"promo":{"id":"4","name":"JUMEIRAH ZABEEL SARAY 5*","image":"","stars":"","highlights":""}},
{"promo":{"id":"5","name":"HABTOOR GRAND BEACH RESORT & SPA","image":"","stars":"","highlights":""}},
{"promo":{"id":"6","name":"HILTON DUBAI JUMEIRAH RESORT","image":"","stars":"","highlights":""}}
]
}
编辑:有人提到我不应该循环数据而不是 data.promos 我试过这个但它没有帮助。我是 JSON 新手:
for (var i=0, len=data.promos.length; i < len; i++)
{
alert(data.promos[i]);
}