美好的一天,
我正在尝试运行一个简单的getJSON以从数据json获取信息,几乎完成了,但是在运行时,在html中获取未定义
这是我的jQuery:
$(document).ready( function() {
$.getJSON("/airport.json?code=bgw", function(data) {
$('#stage').html('<p> Name: ' + data.result.request.code + '</p>');
$.each(data.result.response.airport.pluginData.schedule.arrivals.data, function() {
$("ul").append("<li>Name: "+this['flight.status.text']+"</li><br />");
});
});
});
数据json:
` {
"result": {
"response": {
"airport": {
"pluginData": {
"schedule": {
"arrivals": {
"data": [
{
"flight": {
"status": {
"live": true,
"text": "Estimated 13:44",
"icon": "green",
"estimated": null,
"ambiguous": false
}
}
}
]
}
}
}
}
}
}
}
`
关于我可能做错了什么的任何想法?
最佳答案
您正在以this['flight.status.text']
的形式访问嵌套对象。我相信您想做this.flight.status.text
。看到下面的区别
var data = [{
test: {
deep: {
nested: {
object: 1
}
}
}
}, {
test: {
deep: {
nested: {
object: 2
}
}
}
}];
console.log("Not working");
$.each(data, function() {
console.log(this['test.deep.nested.object']);
});
console.log("--------------");
console.log("Working");
$.each(data, function() {
console.log(this.test.deep.nested.object);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
另外,根据您的JSON,
data.result.request.code
不存在。关于javascript - getjson数组jQuery未定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44483223/