简体   繁体   English

如何获取代码以显示与JSON文件中的值相关联的字符串数据,而不是返回“ undefined”值?

[英]How can I get my code to display the string data associated with the values in the JSON file instead of returning values of “undefined”?

I would like to populate an HTML table with JSON data. 我想用JSON数据填充HTML表。 Problem is I am currently getting values of undefined when I populate the table with data from the URL.My JSON data from the URL is nested so I am not sure if this is what's causing the error. 问题是当我使用来自URL的数据填充表时,当前正在获得undefined的值。来自URL的JSON数据是嵌套的,因此我不确定这是否是导致错误的原因。

   {
"result": [
{
"ITEM_NO": 2,
"ITEM_NME": "PPNA T-Shirt (XS)",
"ITEM_DESC": "Proudly display your support for Baltimore's greenest neighborhood by wearing our T-Shirt when you're out and about!",
"ITEM_ICON_URL": null,
"ITEM_PRICE": "20.00",
"ITEM_CATG_CD": 1,
"ITEM_TYP_CD": null,
"ITEM_AVLB_QTY": "3.00",
"ITEM_STAT_CD": 1,
"item_fee_list": [
{
"FEE_TYP_CD": 1,
"ITEM_FEE_AMT": "4.00",
"ITEM_FEE_PCT": null
}
]
},

{
"ITEM_NO": 76,
"ITEM_NME": "Test Item 1",
"ITEM_DESC": null,
"ITEM_ICON_URL": null,
"ITEM_PRICE": "1.00",
"ITEM_CATG_CD": 1,
"ITEM_TYP_CD": null,
"ITEM_AVLB_QTY": "98.00",
"ITEM_STAT_CD": null,
"item_fee_list": []
}
],
"success": "yes",
"message": "Success"
}

This is my Javascript code to try and read the JSON data to populate the tables. 这是我的Javascript代码,用于尝试读取JSON数据以填充表。 I tried using value.results.ITEM_NO to access the nested data but that did not solve the problem. 我尝试使用value.results.ITEM_NO来访问嵌套数据,但这并不能解决问题。 What I'm I doing wrong? 我做错了什么?

<div class="tab-content">
  <div class="tab-pane active" id="home" role="tabpanel" aria-labelledby="home-tab">

  <table class="table table-bordered table-striped" id="items_table"> 
          <tr>
              <th class="text-center">item no.</th>
              <th class="text-center">item name</th>
              <th class="text-center">item desc </th>

              <!-- <th class="text-center">icon url</th> -->
              <th class="text-center">item price </th>
              <th class="text-center">item ctg cd</th>

              <th class="text-center">item typ cd</th>
              <th class="text-center">item avail qqty</th>
              <th class="text-center">item stat cd</th>

          </tr>

        </table>

      </div>
</div>


<script type="text/javascript">
    $.getJSON("URL", function(data) {

        var items_table = '';
        $.each(data, function(key, value) {

            items_table += '<tr>';

            items_table += '<td>' + value.ITEM_NO + '</td>';
            items_table += '<td>' + value.ITEM_NME + '</td>';
            items_table += '<td>' + value.ITEM_DESC + '</td>';

            // items_table += '<td>' +value.ITEM_ICON_URL+ '</td>';
            items_table += '<td>' + value.ITEM_PRICE + '</td>';
            items_table += '<td>' + value.ITEM_CATG_CD + '</td>';

            items_table += '<td>' + value.ITEM_TYP_CD + '</td>';
            items_table += '<td>' + value.ITEM_AVLB_QTY + '</td>';
            items_table += '<td>' + value.ITEM_STAT_CD + '</td>';



            items_table += '</tr>';


        });

        $('#items_table').append(items_table);


    });
</script>

you have to loop over data.result , as you can see result is the main key which contains the array 您必须遍历data.result ,因为您可以看到result是包含数组的主键

$.each(data.result, function(i,row){
     items_table += '<td>' + row.ITEM_NO + '</td>';
     items_table += '<td>' + row.ITEM_NME + '</td>';
     //.....
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM