无法使用离子2中的键迭代json数组

时间:2017-10-06 05:42:57

标签: ionic-framework ionic2

我在ts文件中有一个json数组

 "stocklist": {
        "1": {
            "product_name": "Chakli(500gm)",
            "item_per_bag": "22",
            "r_rate": "80",
            "in_stock_qty": "129",
            "current_requirement": 998,
            "depreciateStock": -869,
            "depreciateStockValue": -69520
        },
        "12": {
            "product_name": "Khaman mix(200gm)",
            "item_per_bag": "44",
            "r_rate": "32",
            "in_stock_qty": "301",
            "current_requirement": 521,
            "depreciateStock": -220,
            "depreciateStockValue": -7040
        }
    }

我想在html文件中进行迭代。

 <tr *ngFor="let deficit of deficitStockList;">
          <td text-center>{{ deficit.id }}</td>
          <td>{{ deficit.product_name }}</td>
          <td>{{ deficit.in_stock_qty }}</td>
          <td>{{ deficit.current_requirement }}</td>
          <td>{{ deficit.depreciateStock }}</td>
        </tr>

但它显示的错误就像数组不可迭代一样。

这里提到的密钥在Ionic中没用,但我无法从服务器端删除它。

谢谢,

1 个答案:

答案 0 :(得分:0)

将数据格式对象更改为Object of Object,如下所述

"stocklist": [
    {
        "product_name": "Chakli(500gm)",
        "item_per_bag": "22",
        "r_rate": "80",
        "in_stock_qty": "129",
        "current_requirement": 998,
        "depreciateStock": -869,
        "depreciateStockValue": -69520
    },
    {
        "product_name": "Khaman mix(200gm)",
        "item_per_bag": "44",
        "r_rate": "32",
        "in_stock_qty": "301",
        "current_requirement": 521,
        "depreciateStock": -220,
        "depreciateStockValue": -7040
    }
]