gpt4 book ai didi

json - 在 Flutter 上从 Youtube api 解码 Json

转载 作者:IT王子 更新时间:2023-10-29 06:54:28 27 4
gpt4 key购买 nike

我调用 Youtube API 并获取此 Json:

     "kind": "youtube#videoListResponse",
"etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/s7-xmHXpuqQxYzDp_wxhm59K4LE\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#video",
"etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/pajQ7iBy-7A0V_owifxkw-Kbw-Y\"",
"id": "7lCDEYXw3mM",
"snippet": {
"publishedAt": "2012-06-20T23:12:38.000Z",
"channelId": "UC_x5XG1OV2P6uZZ5FSM9Ttw",
"title": "Google I/O 101: Q&A On Using Google APIs",
"description": "Antonio Fuentes speaks to us and takes questions on working with Google APIs and OAuth 2.0.",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/7lCDEYXw3mM/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/7lCDEYXw3mM/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/7lCDEYXw3mM/hqdefault.jpg",
"width": 480,
"height": 360
}

但现在我想解析它并只得到 3 个节点:

  1. 职位
  2. 描述
  3. 默认缩略图的 url

确实我得到了 Json 响应并且可以在日志中看到它,但是每次尝试解析它时都失败了。

这是我的代码:

final response = await http.get(
'https://www.googleapis.com/youtube/v3/videos?id=HEREGOESMYAPIKEY&part=snippet&id=T0Jqdjbed40');

final parsed = json.decode(response.body).cast<Map<String, dynamic>>();
String title = parsed['items']['snippet']['title'];
print(title);
String description = parsed['items']['snippet']['description'];
print(description);
String thumbnail = parsed['items']['snippet']['thumbnails']['default']['url'];
print(thumbnail);

最佳答案

您尝试的操作不适用于 Dart,这不是 javascript。 Dart 有非常强大的类型系统,这很棒。您正在尝试为定义为 String 的变量赋值,但您定义的响应是动态的,因此 Dart 无法验证赋值。 items 也是数组,没有这样的关键 items->snippet。

执行此操作的正确方法是创建模型定义,它将处理反序列化并提供方便的方式来访问您感兴趣的属性。

class YoutubeResponse {

String kind;
String etag;
String nextPageToken;

String regionCode;
List<Item> items;

YoutubeResponse(
{this.kind,
this.etag,
this.nextPageToken,
this.regionCode,
this.items});

Map<String, dynamic> toJson() => {
'kind': kind,
'etag': etag,
'nextPageToken': nextPageToken,
'regionCode': regionCode,
'items': items,
};

factory YoutubeResponse.fromJSON(Map<String, dynamic> YoutubeResponseJson) {

var list = YoutubeResponseJson['items'] as List;
List<Item> itemsList = list.map((i) => Item.fromJSON(i)).toList();

return new YoutubeResponse(
kind: YoutubeResponseJson['kind'],
etag: YoutubeResponseJson['etag'],
nextPageToken: YoutubeResponseJson['nextPageToken'],
regionCode: YoutubeResponseJson['regionCode'],
mPageInfo: pageInfo.fromJSON(YoutubeResponseJson['pageInfo']),
items: itemsList);
}

}

class Item {
String kind;
String etag;
Id id;
Snippet snippet;

Item({this.kind, this.etag, this.id, this.snippet});

Map<String, dynamic> toJson() => {
'kind': kind,
'etag': etag,
'id': id,
'snippet': snippet,
};

factory Item.fromJSON(Map<String, dynamic> ItemJson) {
return Item(
kind: ItemJson['kind'],
etag: ItemJson['etag'],
id: Id.fromJSON(ItemJson['id']),
snippet: Snippet.fromJSON(ItemJson['snippet']),
);
}
}

class Snippet {
String publishedAt;
String channelId;
String title;
String description;
Thumbnails thumbnails;
String channelTitle;
String liveBroadcastContent;

Snippet(
{this.publishedAt,
this.channelId,
this.title,
this.description,
this.thumbnails,
this.channelTitle,
this.liveBroadcastContent});


Map<String, dynamic> toJson() => {
'publishedAt': publishedAt,
'channelId': channelId,
'title': title,
'description': description,
'thumbnails': thumbnails,
'channelTitle': channelTitle,
'liveBroadcastContent': liveBroadcastContent,
};

factory Snippet.fromJSON(Map<String, dynamic> SnippetJson) {


return Snippet(
publishedAt: SnippetJson['publishedAt'],
channelId: SnippetJson['channelId'],
title: SnippetJson['title'],
description: SnippetJson['description'],
thumbnails: Thumbnails.fromJSON(SnippetJson['thumbnails']) ,
channelTitle: SnippetJson['channelTitle'],
liveBroadcastContent: SnippetJson['liveBroadcastContent'],
);
}
}

class Medium {
int height;
int width;
String url;

Medium({this.height, this.width, this.url});

Map<String, dynamic> toJson() => {
'height': height,
'width': width,
'url': url,
};

factory Medium.fromJSON(Map<String, dynamic> MediumJson) {
return Medium(
height: MediumJson['height'],
width: MediumJson['width'],
url: MediumJson['url'],
);
}

}

class High {
int height;
int width;
String url;

High({this.height, this.width, this.url});

Map<String, dynamic> toJson() => {
'height': height,
'width': width,
'url': url,
};

factory High.fromJSON(Map<String, dynamic> HighJson) {
return High(
height: HighJson['height'],
width: HighJson['width'],
url: HighJson['url'],
);
}

}

class Default {
int height;
int width;
String url;

Default({this.height, this.width, this.url});

Map<String, dynamic> toJson() => {
'height': height,
'width': width,
'url': url,
};

factory Default.fromJSON(Map<String, dynamic> defaultJson) {
return Default(
height: defaultJson['height'],
width: defaultJson['width'],
url: defaultJson['url'],
);
}

}

class Thumbnails {
Default mDefault;
Medium medium;
High high;

Thumbnails({this.mDefault, this.medium, this.high});

var data = JsonEncoder().convert("");

Map<String, dynamic> toJson() => {
'default': mDefault,
'medium': medium,
'high': high,
};

factory Thumbnails.fromJSON(Map<String, dynamic> ThumbnailsJson) {
return Thumbnails(
mDefault: Default.fromJSON(ThumbnailsJson['default']),
medium: Medium.fromJSON(ThumbnailsJson['medium']),
high: High.fromJSON(ThumbnailsJson['high']),
);
}
}

既然我们已经描述了 JSON 的格式,我们希望它很容易解析:

YoutubeResponse parsedResponse =
YoutubeResponse.fromJSON(json.decode(response.body));

然后您可以通过 parsedResponse.items 访问项目,然后遍历它们并获取标题、描述等。

关于json - 在 Flutter 上从 Youtube api 解码 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53253169/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com