【发布时间】:2018-02-24 01:20:05
【问题描述】:
我成功地将我的响应打印为来自我的 YouTube JSON url 的字符串,但是当我尝试通过“项目”进行序列化时,我收到以下错误 Unhandled exception:
type 'List' is not a subtype of type 'Map' of 'json' where
List is from dart:core
Map is from dart:core
这是我的代码...
class CardInfo {
//Constructor
String id;
String description;
String role;
//int score;
CardInfo.fromJson(Map json) {
this.id = json['vieoId'];
this.description = json['description'];
this.role = json['title'];
//this.score = json['score'];
}
}
Future getData() async {
String url = 'YouTube url';
var httpClient = createHttpClient();
var response = await httpClient.get(url);
Map data = JSON.decode(response.body);
//String ip = data['items'];
var ci = new CardInfo.fromJson(data['items']);
//print(data['items']);
print(ci.id);
//print(ci.description);
//print(ci.role);
if (!mounted) return;
setState(() {});
}
print(data['items'] 正在打印,但print(ci.id) 或任何 Card Info 变量都会引发上述错误。
****print(data)的日志;
{kind: youtube#searchListResponse, etag: "VPWTmrH7dFmi4s1RqrK4tLejnRI/P9wyOxsXEuXOCvj7znCun2-EykU", nextPageToken: CAMQAA, regionCode: US, pageInfo: {totalResults: 1000000, resultsPerPage: 3}, items: [{kind: youtube#searchResult, etag: "VPWTmrH7dFmi4s1RqrK4tLejnRI/Csl1kQhnOsbs0j4_336zJAN176k", id: {kind: youtube#video, videoId: e3pUxU_bE6w}, snippet: {publishedAt: 2017-09-14T09:43:17.000Z, channelId: UCbD8EppRX3ZwJSou-TVo90A, title: [PRISTIN - We Like] KPOP TV Show | M COUNTDOWN 170914 EP.541, description: KPOP Chart Show M COUNTDOWN | EP.541 - PRISTIN - We Like ▷Watch more video clips: http://MCOUNTDOWN-KPOP2017 [Kor Ver.] 프리티 ..., thumbnails: {default: {url: https://i.ytimg.com/vi/e3pUxU_bE6w/default.jpg, width: 120, height: 90}, medium: {url: https://i.ytimg.com/vi/e3pUxU_bE6w/mqdefault.jpg, width: 320, height: 180}, high: {url: https://i.ytimg.com/vi/e3pUxU_bE6w/hqdefault.jpg, width: 480, height: 360}}, channelTitle: Mnet K-POP, liveBroadcastContent: none}}, {kind: youtube#searchResult, etag: "VPWTmrH7dFmi4s1RqrK4tLejnRI/1JCCNBPNbFeusCp_9-pl4i8q5OU", id: {kind: youtube#video, videoId: Cc4hO9RLdl4}, snippet: {publishedAt: 2017-09-14T10:37:29.000Z, channelId: UCbD8EppRX3ZwJSou-TVo90A, title: [EXO - Power] KPOP TV Show | M COUNTDOWN 170914 EP.541, description: KPOP Chart Show M COUNTDOWN | EP.541 - EXO - Power ▷Watch more video clips: http://MCOUNTDOWN-KPOP2017 [Kor Ver.] Power Up! '#EXO' 여기 ..., thumbnails: {default: {url: https://i.ytimg.com/vi/Cc4hO9RLdl4/default.jpg, width: 120, height: 90}, medium: {url: https://i.ytimg.com/vi/Cc4hO9RLdl4/mqdefault.jpg, width: 320, height: 180}, high: {url: https://i.ytimg.com/vi/Cc4hO9RLdl4/hqdefault.jpg, width: 480, height: 360}}, channelTitle: Mnet K-POP, liveBroadcastContent: none}}, {kind: youtube#searchResult, etag: "VPWTmrH7dFmi4s1RqrK4tLejnRI/ZnYC4e5evyfldkM67HsDuV8Yh3E", id: {kind: youtube#video, videoId: BBcOM25wrVo}, snippet: {publishedAt: 2017-08-18T15:21:48.000Z, channelId: UCtFtO4By4czgkYGvEXvJu0A, title: Kpop Banned Dance: MV vs LIVE, description: Kpop Banned Dance: MV vs LIVE Koreas biggest broadcasting companies has strict rules and standards on what lyrics and dances moves can be performed., thumbnails: {default: {url: https://i.ytimg.com/vi/BBcOM25wrVo/default.jpg, width: 120, height: 90}, medium: {url: https://i.ytimg.com/vi/BBcOM25wrVo/mqdefault.jpg, width: 320, height: 180}, high: {url: https://i.ytimg.com/vi/BBcOM25wrVo/hqdefault.jpg, width: 480, height: 360}}, channelTitle: Kpop Corn, liveBroadcastContent: none}}]}
*** 用 FOR 循环语句更新
这是我的 for loop 的代码,它返回 type 'String' is not a subtype of type 'int' of 'index' 错误...
Map data = JSON.decode(response);
var videos = data['items'];
for (var items in videos['snippet']){
print(items);
}
通过items in videos 循环运行,为我正在寻找的 3 个视频提供了 3 个单独的条目 - 包括 sn-ps。尝试获取单个 sn-ps 失败。请指出我正确的方向。
【问题讨论】:
-
我相信你正在传递
key"items"的value,从外观上看它实际上是List而不是Map,你的构造函数应该收到一个@ 987654336@,但您传递的是List。因此,您会收到错误消息。否则,你能告诉我 print(data) 看看你的数据库是如何构建的吗? -
@aziza 感谢您的以下课程。地图 = NSDictionary 和列表 = NSArray。我注意到有一个
itar和iter来遍历一个列表。我现在正在阅读有关这些的文档。我在我的问题中添加了print(data)。请帮助获取标题、videoId、描述和“中等”缩略图网址的地图。谢谢。 -
据我所知,您正在尝试访问
snippet的值items。因此,您需要逐个索引遍历 data['items'] 索引,然后检查何时到达“sn-p”键,并将此特定映射传递给您的类构造函数。 -
@aziza 试图查看
items以访问我的snippets时抛出了上述错误。请查看。
标签: json serialization dart flutter