迭代内部键:json中的值和显示

时间:2016-09-22 09:37:16

标签: jquery json

我有以下对象:

response = 
    {
        "5": {
            "name": "surgeon bueno",
            "country": "Spain",
            "antiquity": "renewal",
            "amount": "2686.97 USD",
            "sellers": {
                "Frank": "2690.58 USD",
                "Bob": "1690.58 USD",
            }
        },
        "11": {
            "name": "Alex Lloyd",
            "country": "American Samoa",
            "antiquity": "new client",
            "amount": "0.0 USD"
        },
        "12": {
            "name": "alex lloyd",
            "country": "Aruba",
            "antiquity": "new client",
            "amount": "0.0 USD"
        }
    }

我迭代所有值并将它们显示在表中的新行中,如下所示,我想为那些在JSON中有"sellers"的行添加一个新行。像"surgeon bueno"一样,我如何迭代它们并显示一个新行(如果存在的话)?因为他们没有关键名称。

  $.each(response, function(i, item) {
          $('#modal-table tbody').append("<tr><td>" + response[i].name + "</td><td>" + response[i].country + "</td><td>" + response[i].antiquity + "</td><td>" + response[i].amount);
       });

1 个答案:

答案 0 :(得分:0)

使用每个功能的值参数(项目)

$.each(response, function(i, item) {
   $('#modal-table tbody').append("<tr><td>" + item.name + "</td><td>" + item.country + "</td><td>" + item.antiquity + "</td><td>" + item.amount);
});