Aks*_*tel 7 flutter json-serialization flutter-dependencies
考虑以下json:
[
{
"id": 1
"name": "foo"
},
{
"id": 1
"name": "foo"
}
]
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用json_serializable库来解析它。
json.decode(response.body)返回List<dynamic>。
但 json_serializable 自动生成方法fromJson和toJson类型Map<String, dynamic json>。
json_serializable 中是否有任何方法可以List<dynamic> 使用自动生成的方法而不是手动进行解析?
你应该这样尝试;
@JsonSerializable()
class Example{
int id; String name;
Example({this.id, this.name})
}
Run Code Online (Sandbox Code Playgroud)
调用您的 Future 函数并将其保存在变量{parsed}中,然后通过执行以下操作将映射数据转换为列表:
List<Example> result = parsed.map((i) => Example.fromJson(i)).toList();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1789 次 |
| 最近记录: |