将JSON转换为List <map <string,list <string =“” >>>而不过度使用动态

时间:2018-12-28 02:24:16

标签: dart

给出以下JSON字符串

[
    {
        "popular": []
    },
    {
        "recommended": [
            "privacy",
            "security",
            "IFTTT",
            "mobile",
            "location",
            "Pocket",
            "advertising",
            "Instapaper",
            "data",
            "surveillance"
        ]
    }
]

如何将其转换为List<Map<String, List<String>>>而又不过度使用dynamic

这是我目前拥有的:

response // List<dynamic>
        .map((i) => (i as Map<String, dynamic>).map((String key, dynamic value) =>
            MapEntry<String, List<String>>(key, List<String>.from(value))))
        .toList();

1 个答案:

答案 0 :(得分:0)

这是我所能获得的最小的东西-静态类型正确地List<Map<String, List<String>>>,并且与as强制转换只有一点重复。

  var typed = (response as List) //skip this `as` if it's already a List
      .map((v) => (v as Map)
          .map((k, v) => MapEntry<String, List<String>>(k, List.from(v))))
      .toList();

推理将为您填充其余部分。