arrays - Flutter json解码_TypeError(类型'List' 不是类型 'Map 的子类型')
问题描述
所以这个错误我明白它是什么,我需要做什么,但我不明白如何做我想做的事。
所以我使用了一个 html api 服务器调用,它返回一个 jsonEncoded 的 Map 对象列表。
每个列表的大小可以(并且将会)改变,所以我不能有一个只检查和解码前几个或其他东西的函数。每个 Map 对象也可能包含不同的信息,所以我不能让它静态解析。我需要对整个列表进行 jsonDecode,并将其转换回单独的 Map 对象列表,以便我可以在程序的其余部分中使用 Map 对象中包含的信息。
出于隐私原因,我无法提供我收到的对象类型的示例(或任何类似的),对于给您带来的不便以及尝试回答问题的难度增加,我深表歉意。
也就是说,我确实知道我只需要一个函数来检查列表中有多少对象,将 response.body 拆分为多个部分,将每个部分解码为一个 Map 对象,并将其添加到列表或数组中。
您可以提供的任何信息(即使不是特定代码)都会有所帮助,即使只是告诉我应该查看哪些命令。(最好有一个他们的语法示例,但当然不是强制性的)。
如果您需要我可以提供的任何信息,我将很乐意这样做。先感谢您。
解决方案
您的问题的问题是,我们甚至不知道您传入数据的一般结构。作为一个社区,我们不需要知道一个实际的例子。即使是伪造的数据也足够了。
话虽如此,我将尝试为您提供一些示例,说明您的传入数据可能是什么,以及您如何做您想做的事情。
传入 JSON 对象
假设您的传入数据如下所示:
{
"data1": {
"subdata1":"subvalue1",
"subdata2":"subvalue2",
"subdata3":3,
"subdata4":{
"subsubdata1":"subvalue1"
}
},
"data2": {
"subdata1":"subvalue1",
"subdata2":"subvalue2",
"subdata3":3,
"subdata4":{
"subsubdata1":"subvalue1"
}
}
}
在这种情况下,您可以使用 jsonDecode,并且仍然能够计算键并返回关联的对象列表,如下所示:
void consumeResponse(http.Response response) {
// decode the entire response string
final Map<String, dynamic> rawObject = jsonDecode(response.body);
// count the records in the response
final int recordCount = rawObject.keys.length;
// create an array of the data in each top level object key (but you lose the top level key name)
final List<dynamic> records = List<dynamic>.from(rawObject.values);
}
传入的 JSON 对象数组
假设您的 json 实际上已经在一个 json 数组中,并且您的响应如下所示:
[
{
"data1": {
"subdata1":"subvalue1",
"subdata2":"subvalue2",
"subdata3":3,
"subdata4":{
"subsubdata1":"subvalue1"
}
}
},
{
"data2": {
"subdata1":"subvalue1",
"subdata2":"subvalue2",
"subdata3":3,
"subdata4":{
"subsubdata1":"subvalue1"
}
}
}
]
然后你可以用这个函数完成同样的事情:
void consumeResponse(http.Response response) {
// decode the entire response string. this will auto-magically produce a List<dynamic>
final List<dynamic> records = jsonDecode(response.body);
// count the records in the response
final int recordCount = records.length;
}
带有数据子列表的传入 JSON 对象
在某些情况下,您可能会在包装 json 对象中嵌入一个结果列表,该对象还包含一些关于您的查询的额外元数据。在这种情况下,您的数据可能如下所示:
{
"meta": {
"limit": 2,
"offset": 1,
"total": 47
},
"results": [
{
"data1": {
"subdata1":"subvalue1",
"subdata2":"subvalue2",
"subdata3":3,
"subdata4":{
"subsubdata1":"subvalue1"
}
}
},
{
"data2": {
"subdata1":"subvalue1",
"subdata2":"subvalue2",
"subdata3":3,
"subdata4":{
"subsubdata1":"subvalue1"
}
}
}
]
}
在这种情况下,您可能希望在 key 的子列表上运行相同的功能results
。您也可以使用类似的功能来做到这一点:
void consumeResponse(http.Response response) {
// decode the entire response string
final Map<String, dynamic> rawObject = jsonDecode(response.body);
// create the List<dynamic> from the results list in the sub object
final List<dynamic> records = rawObject['results'];
// count the records in the response
final int recordCount = records.length;
}
结束
希望即使您的问题非常模糊,但至少其中一种场景是您可以使用的。如果可以的话,创建一些可以共享的“模拟数据”和/或“模拟代码”总是更好。该社区使用所有可用数据来构建他们的答案。您提供给我们的数据越多,您得到的答案就越好。
推荐阅读
- redux-form - 带有输入组件的 Redux-form 字段在第一个字符上重新呈现,导致键盘折叠
- angular - @angular-devkit/build-angular (nodeSassConfig) 无法通过 npm 下载
- ms-access - Ms Access 2010 DMax 来自另一个表(MS Access 找不到您的表达式中引用的字段“|1”)
- django - 使用 Wagtail API 从页面 slug 返回所有字段
- php - 使用 PHP 在 FTP 帐户上打开文本文件
- python - 如何将 buildbot 属性转换为字符串值
- c - 这个指定的约束是什么意思
- php - 过滤时的 Laravel 雄辩查询
- html - 两个三角形组成一个矩形
- ios - 如何缩短选择和移动组件的触摸时间?(使用 UICollectionViewDragDelegate)