如何在没有密钥的情况下遍历JSON对象?

时间:2019-10-24 03:00:09

标签: json flutter dart

我想通过JSON对象rewards.name创建一个列表,而不用键名5d8c2c245af585d8c2c245af5b

      {
     "response": {
       "currentPage": 1,
    "data": [
          {
            "id": 153289,
            "goalId": 4623,
            "userId": 12,
            "tenantId": 2,
            "department": "Cleaning",
            "status": 0,
            "grandCompletedRate": "0.00",
            "grandCompletedScore": "0.00",
            "goalItemId": 8189,
            "rewards": {
                "5d8c2c245af58": {
                    "name": "Trip to Hokaido, Japan",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "100"
                },
                "5d8c2c245af5b": {
                    "name": "Meal Allowance for 1 month",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "90"
                },
                "5d8c2c245af5d": {
                    "name": "Parking Allowance for 1 month",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "80"
                }
            },
            "intervalStartDate": "2019-10-25 00:00:00",
            "intervalEndDate": "2019-10-25 00:00:00",
            "rewardStatus": 0,
            "rewardedAmount": "0.00",
            "deletedAt": null,
            "totalItemsPoint": 130,
            "goalItemTotal": 13,
            "goalItemCompleted": 0,
            "goalItemPending": 0,
            "pendingApprovalSetting": "1",
            "teamName": null,
            "goal": {
                "id": 4623,
                "name": "Goal 190926",
                "interval": "Daily"
            }
        },
        {
            "id": 153274,
            "goalId": 4617,
            "userId": 12,
            "tenantId": 2,
            "department": "Cleaning",
            "status": 0,
            "grandCompletedRate": "0.00",
            "grandCompletedScore": "0.00",
            "goalItemId": 8178,
            "rewards": {
                "5d8c2ade757eb": {
                    "name": "Trip to Hokaido, Japan",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "100"
                },
                "5d8c2ade757ef": {
                    "name": "Meal Allowance for 1 month",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "90"
                },
                "5d8c2ade757f1": {
                    "name": "Parking Allowance for 1 month",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "80"
                }
            },
            "intervalStartDate": "2019-10-25 00:00:00",
            "intervalEndDate": "2019-10-25 00:00:00",
            "rewardStatus": 0,
            "rewardedAmount": "0.00",
            "deletedAt": null,
            "totalItemsPoint": 130,
            "goalItemTotal": 13,
            "goalItemCompleted": 0,
            "goalItemPending": 0,
            "pendingApprovalSetting": "1",
            "teamName": null,
            "goal": {
                "id": 4617,
                "name": "Goal %",
                "interval": "Daily"
            }
        },
        {
            "id": 152944,
            "goalId": 8,
            "userId": 12,
            "tenantId": 2,
            "department": "Cleaning",
            "status": 0,
            "grandCompletedRate": "0.00",
            "grandCompletedScore": "0.00",
            "goalItemId": 1236,
            "rewards": {
                "5cb55b694c1b0": {
                    "name": "pgr1",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "20"
                },
                "5cb55b694c1b5": {
                    "name": "pgr2",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "40"
                },
                "5cb55b694c1b6": {
                    "name": "pgr3",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "60"
                }
            },
            "intervalStartDate": "2019-10-25 00:00:00",
            "intervalEndDate": "2019-10-25 00:00:00",
            "rewardStatus": 0,
            "rewardedAmount": "0.00",
            "deletedAt": null,
            "totalItemsPoint": 6,
            "goalItemTotal": 3,
            "goalItemCompleted": 0,
            "goalItemPending": 0,
            "pendingApprovalSetting": "1",
            "teamName": null,
            "goal": {
                "id": 8,
                "name": "PG-190416-1233",
                "interval": "Daily"
            }
        },
        {
            "id": 152950,
            "goalId": 21,
            "userId": 12,
            "tenantId": 2,
            "department": "Cleaning",
            "status": 0,
            "grandCompletedRate": "0.00",
            "grandCompletedScore": "0.00",
            "goalItemId": 32,
            "rewards": {
                "5cb5832dcee13": {
                    "name": "ggr1",
                    "description": null,
                    "rewardType": 0,
                    "reward": null,
                    "completionRate": "20"
                },
                "5cb5832dcee16": {
                    "name": "ggr2",
                    "description": null,
                    "rewardType": 0,
                    "reward": null,
                    "completionRate": "40"
                },
                "5cb5832dcee17": {
                    "name": "ggr3",
                    "description": null,
                    "rewardType": 0,
                    "reward": null,
                    "completionRate": "60"
                }
            },
            "intervalStartDate": "2019-10-25 00:00:00",
            "intervalEndDate": "2019-10-25 00:00:00",
            "rewardStatus": 0,
            "rewardedAmount": "0.00",
            "deletedAt": null,
            "totalItemsPoint": 6,
            "goalItemTotal": 3,
            "goalItemCompleted": 0,
            "goalItemPending": 0,
            "pendingApprovalSetting": "1",
            "teamName": null,
            "goal": {
                "id": 21,
                "name": "GG-190416-1523",
                "interval": "Daily"
            }
        },
        {
            "id": 153118,
            "goalId": 4509,
            "userId": 12,
            "tenantId": 2,
            "department": "Cleaning",
            "status": 0,
            "grandCompletedRate": "0.00",
            "grandCompletedScore": "0.00",
            "goalItemId": 7998,
            "rewards": {
                "5d8879398f27e": {
                    "name": "Meal Voucher",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "20"
                },
                "5d8879398f281": {
                    "name": "vimibank RM50",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "40"
                }
            },
            "intervalStartDate": "2019-10-25 00:00:00",
            "intervalEndDate": "2019-10-25 00:00:00",
            "rewardStatus": 0,
            "rewardedAmount": "0.00",
            "deletedAt": null,
            "totalItemsPoint": 375,
            "goalItemTotal": 4,
            "goalItemCompleted": 0,
            "goalItemPending": 0,
            "pendingApprovalSetting": "1",
            "teamName": null,
            "goal": {
                "id": 4509,
                "name": "Goal's in 31 September",
                "interval": "Daily"
            }
        },
        {
            "id": 153133,
            "goalId": 4511,
            "userId": 12,
            "tenantId": 2,
            "department": "Cleaning",
            "status": 0,
            "grandCompletedRate": "0.00",
            "grandCompletedScore": "0.00",
            "goalItemId": 8014,
            "rewards": {
                "5d8881526c231": {
                    "name": "Meal Voucher",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "20"
                },
                "5d8881526c236": {
                    "name": "vimibank RM50",
                    "description": null,
                    "rewardType": "1",
                    "reward": null,
                    "completionRate": "40"
                }
            },
            "intervalStartDate": "2019-10-25 00:00:00",
            "intervalEndDate": "2019-10-25 00:00:00",
            "rewardStatus": 0,
            "rewardedAmount": "0.00",
            "deletedAt": null,
            "totalItemsPoint": 30,
            "goalItemTotal": 2,
            "goalItemCompleted": 0,
            "goalItemPending": 0,
            "pendingApprovalSetting": "1",
            "teamName": "GT-190923 Project in Kuala Lumpur for new branch",
            "goal": {
                "id": 4511,
                "name": "Goal 1-GT-190923",
                "interval": "Daily"
            }
        },
        {
            "id": 153140,
            "goalId": 4512,
            "userId": 12,
            "tenantId": 2,
            "department": "Cleaning",
            "status": 0,
            "grandCompletedRate": "0.00",
            "grandCompletedScore": "0.00",
            "goalItemId": 8015,
            "rewards": {
                "5d8881527479c": {
                    "name": "Meal Voucher",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "25"
                },
                "5d888152747a0": {
                    "name": "vimibank RM75",
                    "description": null,
                    "rewardType": "1",
                    "reward": null,
                    "completionRate": "50"
                }
            },
            "intervalStartDate": "2019-10-25 00:00:00",
            "intervalEndDate": "2019-10-25 00:00:00",
            "rewardStatus": 0,
            "rewardedAmount": "0.00",
            "deletedAt": null,
            "totalItemsPoint": 3,
            "goalItemTotal": 2,
            "goalItemCompleted": 0,
            "goalItemPending": 0,
            "pendingApprovalSetting": "1",
            "teamName": "GT-190923 Project in Kuala Lumpur for new branch",
            "goal": {
                "id": 4512,
                "name": "Goal 2",
                "interval": "Daily"
            }
        },
        {
            "id": 152993,
            "goalId": 4434,
            "userId": 12,
            "tenantId": 2,
            "department": "Cleaning",
            "status": 0,
            "grandCompletedRate": "0.00",
            "grandCompletedScore": "0.00",
            "goalItemId": 8011,
            "rewards": null,
            "intervalStartDate": "2019-10-25 00:00:00",
            "intervalEndDate": "2019-10-25 00:00:00",
            "rewardStatus": 0,
            "rewardedAmount": "0.00",
            "deletedAt": null,
            "totalItemsPoint": 133,
            "goalItemTotal": 4,
            "goalItemCompleted": 0,
            "goalItemPending": 0,
            "pendingApprovalSetting": "1",
            "teamName": null,
            "goal": {
                "id": 4434,
                "name": "HG-190920-1754",
                "interval": "Daily"
            }
        },
        {
            "id": 139106,
            "goalId": 1,
            "userId": 12,
            "tenantId": 2,
            "department": "Cleaning",
            "status": 0,
            "grandCompletedRate": "0.00",
            "grandCompletedScore": "0.00",
            "goalItemId": 6661,
            "rewards": {
                "5cb5595c50832": {
                    "name": "Meal Voucher",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "20"
                },
                "5cb5595c50836": {
                    "name": "vimibank RM50",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "40"
                }
            },
            "intervalStartDate": "2019-10-21 00:00:00",
            "intervalEndDate": "2019-10-27 00:00:00",
            "rewardStatus": 0,
            "rewardedAmount": "0.00",
            "deletedAt": null,
            "totalItemsPoint": 30,
            "goalItemTotal": 2,
            "goalItemCompleted": 0,
            "goalItemPending": 0,
            "pendingApprovalSetting": "1",
            "teamName": null,
            "goal": {
                "id": 1,
                "name": "Goal 1",
                "interval": "Weekly"
            }
        },
        {
            "id": 139117,
            "goalId": 2,
            "userId": 12,
            "tenantId": 2,
            "department": "Cleaning",
            "status": 0,
            "grandCompletedRate": "0.00",
            "grandCompletedScore": "0.00",
            "goalItemId": 4,
            "rewards": {
                "5cb5598746005": {
                    "name": "Meal Voucher",
                    "description": null,
                    "rewardType": 0,
                    "reward": null,
                    "completionRate": "25"
                },
                "5cb5598746009": {
                    "name": "vimibank RM75",
                    "description": null,
                    "rewardType": 0,
                    "reward": null,
                    "completionRate": "50"
                }
            },
            "intervalStartDate": "2019-10-21 00:00:00",
            "intervalEndDate": "2019-10-27 00:00:00",
            "rewardStatus": 0,
            "rewardedAmount": "0.00",
            "deletedAt": null,
            "totalItemsPoint": 3,
            "goalItemTotal": 2,
            "goalItemCompleted": 0,
            "goalItemPending": 0,
            "pendingApprovalSetting": "1",
            "teamName": null,
            "goal": {
                "id": 2,
                "name": "Goal 2",
                "interval": "Weekly"
            }
        },
        {
            "id": 133427,
            "goalId": 2,
            "userId": 12,
            "tenantId": 2,
            "department": "Cleaning",
            "status": 0,
            "grandCompletedRate": "0.00",
            "grandCompletedScore": "0.00",
            "goalItemId": 4,
            "rewards": {
                "5cb5598746005": {
                    "name": "Meal Voucher",
                    "description": null,
                    "rewardType": 0,
                    "reward": null,
                    "completionRate": "25"
                },
                "5cb5598746009": {
                    "name": "vimibank RM75",
                    "description": null,
                    "rewardType": 0,
                    "reward": null,
                    "completionRate": "50"
                }
            },
            "intervalStartDate": "2019-10-14 00:00:00",
            "intervalEndDate": "2019-10-20 00:00:00",
            "rewardStatus": 0,
            "rewardedAmount": "0.00",
            "deletedAt": null,
            "totalItemsPoint": 3,
            "goalItemTotal": 2,
            "goalItemCompleted": 0,
            "goalItemPending": 0,
            "pendingApprovalSetting": "1",
            "teamName": null,
            "goal": {
                "id": 2,
                "name": "Goal 2",
                "interval": "Weekly"
            }
        },
        {
            "id": 133416,
            "goalId": 1,
            "userId": 12,
            "tenantId": 2,
            "department": "Cleaning",
            "status": 0,
            "grandCompletedRate": "0.00",
            "grandCompletedScore": "0.00",
            "goalItemId": 6661,
            "rewards": {
                "5cb5595c50832": {
                    "name": "Meal Voucher",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "20"
                },
                "5cb5595c50836": {
                    "name": "vimibank RM50",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "40"
                }
            },
            "intervalStartDate": "2019-10-14 00:00:00",
            "intervalEndDate": "2019-10-20 00:00:00",
            "rewardStatus": 0,
            "rewardedAmount": "0.00",
            "deletedAt": null,
            "totalItemsPoint": 30,
            "goalItemTotal": 2,
            "goalItemCompleted": 0,
            "goalItemPending": 0,
            "pendingApprovalSetting": "1",
            "teamName": null,
            "goal": {
                "id": 1,
                "name": "Goal 1",
                "interval": "Weekly"
            }
        },
        {
            "id": 113101,
            "goalId": 4580,
            "userId": 12,
            "tenantId": 2,
            "department": "Cleaning",
            "status": 0,
            "grandCompletedRate": "0.00",
            "grandCompletedScore": "0.00",
            "goalItemId": 8125,
            "rewards": null,
            "intervalStartDate": "2019-10-01 00:00:00",
            "intervalEndDate": "2019-10-31 00:00:00",
            "rewardStatus": 0,
            "rewardedAmount": "0.00",
            "deletedAt": null,
            "totalItemsPoint": 101,
            "goalItemTotal": 2,
            "goalItemCompleted": 0,
            "goalItemPending": 0,
            "pendingApprovalSetting": "1",
            "teamName": null,
            "goal": {
                "id": 4580,
                "name": "Goal Testing in %",
                "interval": "Monthly"
            }
        },
        {
            "id": 151755,
            "goalId": 4989,
            "userId": 12,
            "tenantId": 2,
            "department": "Cleaning",
            "status": 0,
            "grandCompletedRate": "0.00",
            "grandCompletedScore": "0.00",
            "goalItemId": 8740,
            "rewards": {
                "5dafd142455d9": {
                    "name": "Trip to Hokaido, Japan",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "100"
                },
                "5dafd142455dc": {
                    "name": "Meal Allowance for 1 month",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "90"
                },
                "5dafd142455de": {
                    "name": "Parking Allowance for 1 month",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "80"
                }
            },
            "intervalStartDate": "2019-10-01 00:00:00",
            "intervalEndDate": "2019-10-31 00:00:00",
            "rewardStatus": 0,
            "rewardedAmount": "0.00",
            "deletedAt": null,
            "totalItemsPoint": 130,
            "goalItemTotal": 13,
            "goalItemCompleted": 0,
            "goalItemPending": 0,
            "pendingApprovalSetting": "1",
            "teamName": null,
            "goal": {
                "id": 4989,
                "name": "Goal Achieved 01",
                "interval": "Monthly"
            }
           },
        {
            "id": 107936,
            "goalId": 1403,
            "userId": 12,
            "tenantId": 2,
            "department": "Cleaning",
            "status": 0,
            "grandCompletedRate": "0.00",
            "grandCompletedScore": "0.00",
            "goalItemId": 7820,
            "rewards": {
                "5d30636e3062b": {
                    "name": "Meal Voucher",
                    "description": null,
                    "rewardType": "0",
                    "reward": null,
                    "completionRate": "25"
                },
                "5d30636e3062f": {
                    "name": "vimibank RM75",
                    "description": null,
                    "rewardType": "1",
                    "reward": null,
                    "completionRate": "50"
                }
            },
            "intervalStartDate": "2019-10-01 00:00:00",
            "intervalEndDate": "2019-10-31 00:00:00",
            "rewardStatus": 0,
            "rewardedAmount": "0.00",
            "deletedAt": null,
            "totalItemsPoint": 78,
            "goalItemTotal": 12,
            "goalItemCompleted": 0,
            "goalItemPending": 0,
            "pendingApprovalSetting": "1",
            "teamName": null,
            "goal": {
                "id": 1403,
                "name": "190920-Goal's Testing for Character\"s",
                "interval": "Monthly"
            }
        }
    ],

  }
  }

我希望创建一个奖励名称列表。

1 个答案:

答案 0 :(得分:0)

您可以将rewards属性转换为Map<String, Map<String, String>>,这基本上使它成为嵌套地图的地图。之后,您可以遍历奖励对象的值并将其fold(展平)为所有奖励名称的单个值。

  final dataItems = responses['response']['data'] as List<Map<String, dynamic>>;
  final rewardNames = dataItems.fold(
      List<String>(),
      (rewards, items) => rewards
        ..addAll((items['rewards'] as Map<String, Map<String, String>>)
            .values
            .map((reward) => reward['name'])));
  //rewardNames.forEach((name) => print(name));

要在ListView中使用:

ListView.builder(
  padding: const EdgeInsets.all(0),
  itemCount: rewardNames.length,
  itemBuilder: (BuildContext context, int index) {
    return Text(rewardNames[index]);
  }
);