【发布时间】:2018-10-05 09:32:48
【问题描述】:
我正在从一个 url 加载以下 json 文件:
{
"Airports": [
{
"listing": "East 34th Street Heliport",
"iata": "TSS",
"type": "heliport",
"size": "tiny"
},
{
"listing": "Blaine Municipal Airport",
"iata": "BWS",
"type": "closed",
"size": "small"
},
{
"listing": "Toronto City Airport",
"iata": "YTZ",
"type": "airport",
"size": "medium"
},
{
"listing": "Amsterdam Airport Schiphol",
"iata": "AMS",
"type": "airport",
"size": "large"
},
{
"listing": "Detroit County Airport",
"iata": "DTW",
"type": "airport",
"size": "large"
}
]
}
我想遍历 Airports 数组并在 DOM 上显示所有键名和值。我使用 jquery mobile 在 .each() 循环中执行此操作:
if(pageID == "page1"){
var pageTitle="Error";
//temp var to hold collapsible HTML
var colItem="";
$.ajax({
url:"some_url",
method:"GET",
cache:false,
dataType:"json",
success:function(data){
pageTitle = (Object.keys(data)[0]).toUpperCase();
$(data.Airports).each(function(index,value){
//build all the needed collapsibles
colItem +=
"<div data-role='collapsible'><h2>"
+ value.listing +
"</h2> <p>"
+ +
"</p> <p>"
+ +
"</p> <p>"
+ +
"</p></div>";
});
}
});
有没有一种方法可以在不引用键值的情况下执行此操作,例如我使用 value.listing 所做的,而是像数组一样遍历它并以这种方式获取所有值。
我正在寻找类似这样的最终结果:
East 34th Street Heliport
iata TSS
type heliport
size tiny
【问题讨论】:
标签: arrays json html jquery-mobile