9

考虑以下 json:

[
 {
  "id": 1
  "name": "foo"
 },
 {
  "id": 1
  "name": "foo"
 }
]

我正在尝试使用json_serializable库来解析它。

json.decode(response.body)返回List<dynamic>

但是 json_serializable 会自动生成方法fromJsontoJson使用 type Map<String, dynamic json>

json_serializable 中是否有任何方法可以List<dynamic> 使用自动生成的方法而不是手动进行解析?

4

1 回答 1

4

你应该这样试试;

@JsonSerializable()
class Example{
    int id; String name;
    Example({this.id, this.name})
}

调用您的 Future 函数并将其保存在变量{parsed}中,然后通过执行以下操作将映射数据转换为列表:

List<Example> result = parsed.map((i) => Example.fromJson(i)).toList();
于 2019-10-09T09:57:53.067 回答