【发布时间】:2021-09-10 12:27:08
【问题描述】:
我需要连接两个 json 响应(将第二个附加到第一个) 因为我有一个模型类,它混合了第一个和第二个响应的字段。
我的代码:
List nextR = [];
final response = await dio.get(URL);
final result = response.data;
final response2 = await dio.get(URL);
final result2 = response2.data;
nextR.add(result);
我尝试了nextR.join(...) 和nextR.extend(...),但它不起作用
现在我收到此错误(对于 Ezra 解决方案)
Unhandled Exception: FormatException: Unexpected character (at character 2)
E/flutter (26428): {example:.......
第一反应是:
{
"SData": {
"series": "example",
"Table": {
"year": "2021",
"Info": [
{
"year": "2021",
"turn": "1",
"date": "2021-01-01",
"time": "23:00:00Z"
},
]
}
}
}
第二个是:
{
"SData": {
"series": "example",
"Table": {
"year": "2021",
"Info": [
{
"year": "2021",
"Results": [
{
"number": "18",
"ac": "none",
"Time": {
"time": "no time"
},
},
]
}
]
}
}
}
我想将“结果”合并到第一个响应中
【问题讨论】:
-
发生这种情况是因为您使用了
final尝试使用var -
anirudh,我认为这不是他们要问的——我认为他们想将 result 和 result2 添加到 nextR 列表中。如果是正确的,那么它与 json 关系不大。
-
@Ezra 但用户已经声明了
nextR.add(result); -
是的,我想他们想同时添加两者,所以他们需要添加
result2?我可能是错的。 @lukko,请澄清。 -
我已经用其他解释更新了我的答案。 @anirudh,这是你认为他们的意思吗?