使用if语句从JSON获取值,其中数据之一仅显示为[[]

时间:2019-01-10 07:01:02

标签: json dart flutter

我陷入困境,需要任何帮助来解决此问题。我想从“ units.number”和“ units.type”中获取数据。但是在我的JSON数据中,有些单位的值为'[]'。谢谢你。

这是我的JSON数据:

{
    "data": {
        "shipment_summary": {
            "destinations": [
                ...
                {
                    "orders": [
                        {
                            "id": "31818",
                            "number": "84092202",
                            "units": []
                        },
                        {
                            "id": "31819",
                            "number": "803839747",
                            "units": [
                                {
                                    "number": "510812021",
                                    "type": "BUNDLE"
                                },
                                {
                                    "number": "510812040",
                                    "type": "CARTON"
                                },
                                {
                                    "number": "510812041",
                                    "type": "CARTON"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    }
}

我想要这样的数据:

订单1:

此订单没有任何单位。

订单2:

510812021-捆绑包

510812040-纸箱

510812041-纸箱

1 个答案:

答案 0 :(得分:0)

尝试

var data = [{
                "shipment_summary": {
                    "destinations": [

                        {
                            "orders": [
                        {
                            "id": "31818",
                            "number": "84092202",
                            "units": []
                        },
                        {
                            "id": "31819",
                            "number": "803839747",
                            "units": [
                {
                    "number": "510812021",
                    "type": "BUNDLE"
                },
                {
                    "number": "510812040",
                    "type": "CARTON"
                },
                {
                    "number": "510812041",
                    "type": "CARTON"
                }
                            ]
                        }
                            ]
                        }
                    ]
                }
            }]


            var data1 = data[0].shipment_summary.destinations[0].orders;

            for (var i = 0; data1.length > i; i++) {
                for (var j = 0; data1[i].units.length > j; j++) {
                    console.log(data1[i].units[j].number + '-' + data1[i].units[j].type);
                }
            }