如何迭代JSON数组

时间:2018-04-25 02:38:09

标签: html arrays json html5 jquery-mobile

我正在从网址加载以下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在

    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

2 个答案:

答案 0 :(得分:0)

你可以这样做。动态创建HTML字符串,然后将其添加到主HTML页面:

var jsonData = {
    "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"
      }
    ]
};

var nHTML = '';
jsonData.Airports.forEach(function(airport){
  var paragraph = '';
  paragraph+='<p>iata:   '+airport.iata+'</p>' + 
             '<p>type:   '+airport.type+'</p>' +
             '<p>size:   '+airport.size+'</p>';
  var colItem = '<div data-role="collapsible">' +airport.listing+ '<h2></h2>'+paragraph+'</div>';
  nHTML+=colItem;
});

$('#container').html(nHTML);
#container div{
  margin: 7px;
  background: grey;
  padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='container'></div>

答案 1 :(得分:0)

这是一个非常简单的JSFiddle,你可以如何做到这一点:

https://jsfiddle.net/8euuoccf/

var jsonData = {
  "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"
    }
  ]
};

$(document).ready(function() {

    // Iterate over each airport
    jsonData.Airports.forEach(airport => {
      colItem = `<div data-role='collapsible'><h2>${airport.listing}</h2>`;

      // Iterate over each airport key
      Object.keys(airport).forEach(key => {
        colItem += `<p>${key}: ${airport[key]}</p>`;
      });
      colItem += '</div>';

      // Finally, append it to body
      var html = $.parseHTML(colItem);
      $('body').append(colItem);
    });
});

有几种方法可以迭代Object键/值。 Object.entries()是另一种选择,但要记住它在IE中不受支持。在这个例子中,我使用了Object.keys(),然后我访问了相应的值。如果您想在DOM中使用其他格式(例如表格),请选择keyairport[key]并根据您的喜好添加它们。