我想要这个json的“标题”
我只想要项目中的标题。 我可以通过诸如kind或etag之类的项获取每个数据,但是项为null。 如何获得标题?
var data = await http.get("https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={id}&maxResults=5&order=date&type=video&key={key}");
var jsonData = json.decode(data.body);
String title = jsonData["items"][0]["title"];
{
"kind": "youtube#searchListResponse",
"etag": "\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/g32qWHUfh9ZGLFfaJ4eAIWqe5As\"",
"nextPageToken": "CAUQAA",
"regionCode": "DE",
"pageInfo": {
"totalResults": 207,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/IPag1MLLCZWNOan8xL200XduRjk\"",
"id": {
"kind": "youtube#video",
"videoId": "iWkAYZyrrWc"
},
"snippet": {
"publishedAt": "2019-07-24T15:59:15.000Z",
"channelId": "UC7TZhmZOk5nxVjBozb2EW4w",
"title": "Afterbuy Statistiken - Performance Analyse und Controlling für Onlinehändler!",
"description": "Die Afterbuy Statistik ist ein Auswertungstool zur Analyse der Performance eines Onlinehändlers im E-Commerce Markt. Wenn du den Mehrwert dieser Afterbuy ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/iWkAYZyrrWc/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/iWkAYZyrrWc/mqdefault.jpg",
"width": 320,
"height": 180
}
}
}
}
]
};
}
答案 0 :(得分:2)
您错过了片段:
var title = jsonData()["items"][0].snippet["title"];
console.log(title);
function jsonData() {
return {
"kind": "youtube#searchListResponse",
"etag": "\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/g32qWHUfh9ZGLFfaJ4eAIWqe5As\"",
"nextPageToken": "CAUQAA",
"regionCode": "DE",
"pageInfo": {
"totalResults": 207,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/IPag1MLLCZWNOan8xL200XduRjk\"",
"id": {
"kind": "youtube#video",
"videoId": "iWkAYZyrrWc"
},
"snippet": {
"publishedAt": "2019-07-24T15:59:15.000Z",
"channelId": "UC7TZhmZOk5nxVjBozb2EW4w",
"title": "Afterbuy Statistiken - Performance Analyse und Controlling für Onlinehändler!",
"description": "Die Afterbuy Statistik ist ein Auswertungstool zur Analyse der Performance eines Onlinehändlers im E-Commerce Markt. Wenn du den Mehrwert dieser Afterbuy ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/iWkAYZyrrWc/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/iWkAYZyrrWc/mqdefault.jpg",
"width": 320,
"height": 180
}
}
}
}
]
};
}
答案 1 :(得分:0)
您可以使用this网站生成用于响应的Model类。
或者您也可以查看以下内容:JSON and serialization
并按如下所示使用它:
// If server returns an OK response, parse the JSON.
return YourModel.fromJson(json.decode(data.body));
答案 2 :(得分:0)
如果您只想标题而又不想创建模型(您确实应该使用IMO)
伪代码
curl -v localhost:8080
-H 'Content-Type: multipart/form-data'
--form 'command={"test": "hello"};type=application/json'
--form 'certificates=@example.jpg;type=image/jpeg'
答案 3 :(得分:0)
您应按如下所示创建模型。
//要解析此JSON数据,请执行 // //最终结果= resultFromJson(jsonString);
2nd Way:
import * as abc from './abc'
//Then use it like:
abc.function1();
下一步,
import 'dart:convert';
class Result {
String kind;
String etag;
String nextPageToken;
String regionCode;
PageInfo pageInfo;
List<Item> items;
Result({
this.kind,
this.etag,
this.nextPageToken,
this.regionCode,
this.pageInfo,
this.items,
});
factory Result.fromRawJson(String str) => Result.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory Result.fromJson(Map<String, dynamic> json) => new Result(
kind: json["kind"] == null ? null : json["kind"],
etag: json["etag"] == null ? null : json["etag"],
nextPageToken: json["nextPageToken"] == null ? null : json["nextPageToken"],
regionCode: json["regionCode"] == null ? null : json["regionCode"],
pageInfo: json["pageInfo"] == null ? null : PageInfo.fromJson(json["pageInfo"]),
items: json["items"] == null ? null : new List<Item>.from(json["items"].map((x) => Item.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"kind": kind == null ? null : kind,
"etag": etag == null ? null : etag,
"nextPageToken": nextPageToken == null ? null : nextPageToken,
"regionCode": regionCode == null ? null : regionCode,
"pageInfo": pageInfo == null ? null : pageInfo.toJson(),
"items": items == null ? null : new List<dynamic>.from(items.map((x) => x.toJson())),
};
}
class Item {
String kind;
String etag;
Id id;
Snippet snippet;
Item({
this.kind,
this.etag,
this.id,
this.snippet,
});
factory Item.fromRawJson(String str) => Item.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory Item.fromJson(Map<String, dynamic> json) => new Item(
kind: json["kind"] == null ? null : json["kind"],
etag: json["etag"] == null ? null : json["etag"],
id: json["id"] == null ? null : Id.fromJson(json["id"]),
snippet: json["snippet"] == null ? null : Snippet.fromJson(json["snippet"]),
);
Map<String, dynamic> toJson() => {
"kind": kind == null ? null : kind,
"etag": etag == null ? null : etag,
"id": id == null ? null : id.toJson(),
"snippet": snippet == null ? null : snippet.toJson(),
};
}
class Id {
String kind;
String videoId;
Id({
this.kind,
this.videoId,
});
factory Id.fromRawJson(String str) => Id.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory Id.fromJson(Map<String, dynamic> json) => new Id(
kind: json["kind"] == null ? null : json["kind"],
videoId: json["videoId"] == null ? null : json["videoId"],
);
Map<String, dynamic> toJson() => {
"kind": kind == null ? null : kind,
"videoId": videoId == null ? null : videoId,
};
}
class Snippet {
DateTime publishedAt;
String channelId;
String title;
String description;
Thumbnails thumbnails;
Snippet({
this.publishedAt,
this.channelId,
this.title,
this.description,
this.thumbnails,
});
factory Snippet.fromRawJson(String str) => Snippet.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory Snippet.fromJson(Map<String, dynamic> json) => new Snippet(
publishedAt: json["publishedAt"] == null ? null : DateTime.parse(json["publishedAt"]),
channelId: json["channelId"] == null ? null : json["channelId"],
title: json["title"] == null ? null : json["title"],
description: json["description"] == null ? null : json["description"],
thumbnails: json["thumbnails"] == null ? null : Thumbnails.fromJson(json["thumbnails"]),
);
Map<String, dynamic> toJson() => {
"publishedAt": publishedAt == null ? null : publishedAt.toIso8601String(),
"channelId": channelId == null ? null : channelId,
"title": title == null ? null : title,
"description": description == null ? null : description,
"thumbnails": thumbnails == null ? null : thumbnails.toJson(),
};
}
class Thumbnails {
Default thumbnailsDefault;
Default medium;
Thumbnails({
this.thumbnailsDefault,
this.medium,
});
factory Thumbnails.fromRawJson(String str) => Thumbnails.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory Thumbnails.fromJson(Map<String, dynamic> json) => new Thumbnails(
thumbnailsDefault: json["default"] == null ? null : Default.fromJson(json["default"]),
medium: json["medium"] == null ? null : Default.fromJson(json["medium"]),
);
Map<String, dynamic> toJson() => {
"default": thumbnailsDefault == null ? null : thumbnailsDefault.toJson(),
"medium": medium == null ? null : medium.toJson(),
};
}
class Default {
String url;
int width;
int height;
Default({
this.url,
this.width,
this.height,
});
factory Default.fromRawJson(String str) => Default.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory Default.fromJson(Map<String, dynamic> json) => new Default(
url: json["url"] == null ? null : json["url"],
width: json["width"] == null ? null : json["width"],
height: json["height"] == null ? null : json["height"],
);
Map<String, dynamic> toJson() => {
"url": url == null ? null : url,
"width": width == null ? null : width,
"height": height == null ? null : height,
};
}
class PageInfo {
int totalResults;
int resultsPerPage;
PageInfo({
this.totalResults,
this.resultsPerPage,
});
factory PageInfo.fromRawJson(String str) => PageInfo.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory PageInfo.fromJson(Map<String, dynamic> json) => new PageInfo(
totalResults: json["totalResults"] == null ? null : json["totalResults"],
resultsPerPage: json["resultsPerPage"] == null ? null : json["resultsPerPage"],
);
Map<String, dynamic> toJson() => {
"totalResults": totalResults == null ? null : totalResults,
"resultsPerPage": resultsPerPage == null ? null : resultsPerPage,
};
}
它应该在此时工作
答案 4 :(得分:0)
您可以通过这种方式进行。
import 'dart:convert';
import 'json_objects.dart';
main(List<String> args) {
var json = jsonDecode(_source) as Map<String, dynamic>;
var response = Response1.fromJson(json);
for (var item in response.items) {
print(item.snippet.title);
}
}
final _source = r'''
{
"kind": "youtube#searchListResponse",
"etag": "\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/g32qWHUfh9ZGLFfaJ4eAIWqe5As\"",
"nextPageToken": "CAUQAA",
"regionCode": "DE",
"pageInfo": {
"totalResults": 207,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/IPag1MLLCZWNOan8xL200XduRjk\"",
"id": {
"kind": "youtube#video",
"videoId": "iWkAYZyrrWc"
},
"snippet": {
"publishedAt": "2019-07-24T15:59:15.000Z",
"channelId": "UC7TZhmZOk5nxVjBozb2EW4w",
"title": "Afterbuy Statistiken - Performance Analyse und Controlling für Onlinehändler!",
"description": "Die Afterbuy Statistik ist ein Auswertungstool zur Analyse der Performance eines Onlinehändlers im E-Commerce Markt. Wenn du den Mehrwert dieser Afterbuy ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/iWkAYZyrrWc/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/iWkAYZyrrWc/mqdefault.jpg",
"width": 320,
"height": 180
}
}
}
}
]
}''';
结果:
Afterbuy Statistiken - Performance Analyse und Controlling für Onlinehändler!
使用的数据模型(由工具自动生成)。
class Response1 {
final String etag;
final List<Response1Items> items;
final String kind;
final String nextPageToken;
final Response1PageInfo pageInfo;
final String regionCode;
Response1(
{this.etag,
this.items,
this.kind,
this.nextPageToken,
this.pageInfo,
this.regionCode});
factory Response1.fromJson(Map<String, dynamic> json) {
return Response1(
etag: json['etag'] as String,
items: _toObjectList(json['items'], (e) => Response1Items.fromJson(e)),
kind: json['kind'] as String,
nextPageToken: json['nextPageToken'] as String,
pageInfo:
_toObject(json['pageInfo'], (e) => Response1PageInfo.fromJson(e)),
regionCode: json['regionCode'] as String,
);
}
Map<String, dynamic> toJson() {
return {
'etag': etag,
'items': _fromList(items, (e) => e.toJson()),
'kind': kind,
'nextPageToken': nextPageToken,
'pageInfo': pageInfo?.toJson(),
'regionCode': regionCode,
};
}
}
class Response1Items {
final String etag;
final Response1ItemsId id;
final String kind;
final Response1ItemsSnippet snippet;
Response1Items({this.etag, this.id, this.kind, this.snippet});
factory Response1Items.fromJson(Map<String, dynamic> json) {
return Response1Items(
etag: json['etag'] as String,
id: _toObject(json['id'], (e) => Response1ItemsId.fromJson(e)),
kind: json['kind'] as String,
snippet:
_toObject(json['snippet'], (e) => Response1ItemsSnippet.fromJson(e)),
);
}
Map<String, dynamic> toJson() {
return {
'etag': etag,
'id': id?.toJson(),
'kind': kind,
'snippet': snippet?.toJson(),
};
}
}
class Response1ItemsId {
final String kind;
final String videoId;
Response1ItemsId({this.kind, this.videoId});
factory Response1ItemsId.fromJson(Map<String, dynamic> json) {
return Response1ItemsId(
kind: json['kind'] as String,
videoId: json['videoId'] as String,
);
}
Map<String, dynamic> toJson() {
return {
'kind': kind,
'videoId': videoId,
};
}
}
class Response1ItemsSnippet {
final String channelId;
final String description;
final DateTime publishedAt;
final Response1ItemsSnippetThumbnails thumbnails;
final String title;
Response1ItemsSnippet(
{this.channelId,
this.description,
this.publishedAt,
this.thumbnails,
this.title});
factory Response1ItemsSnippet.fromJson(Map<String, dynamic> json) {
return Response1ItemsSnippet(
channelId: json['channelId'] as String,
description: json['description'] as String,
publishedAt: _toDateTime(json['publishedAt']),
thumbnails: _toObject(json['thumbnails'],
(e) => Response1ItemsSnippetThumbnails.fromJson(e)),
title: json['title'] as String,
);
}
Map<String, dynamic> toJson() {
return {
'channelId': channelId,
'description': description,
'publishedAt': _fromDateTime(publishedAt),
'thumbnails': thumbnails?.toJson(),
'title': title,
};
}
}
class Response1ItemsSnippetThumbnail {
final int height;
final String url;
final int width;
Response1ItemsSnippetThumbnail({this.height, this.url, this.width});
factory Response1ItemsSnippetThumbnail.fromJson(Map<String, dynamic> json) {
return Response1ItemsSnippetThumbnail(
height: json['height'] as int,
url: json['url'] as String,
width: json['width'] as int,
);
}
Map<String, dynamic> toJson() {
return {
'height': height,
'url': url,
'width': width,
};
}
}
class Response1ItemsSnippetThumbnails {
final Response1ItemsSnippetThumbnail default_;
final Response1ItemsSnippetThumbnail medium;
Response1ItemsSnippetThumbnails({this.default_, this.medium});
factory Response1ItemsSnippetThumbnails.fromJson(Map<String, dynamic> json) {
return Response1ItemsSnippetThumbnails(
default_: _toObject(
json['default'], (e) => Response1ItemsSnippetThumbnail.fromJson(e)),
medium: _toObject(
json['medium'], (e) => Response1ItemsSnippetThumbnail.fromJson(e)),
);
}
Map<String, dynamic> toJson() {
return {
'default': default_?.toJson(),
'medium': medium?.toJson(),
};
}
}
class Response1PageInfo {
final int resultsPerPage;
final int totalResults;
Response1PageInfo({this.resultsPerPage, this.totalResults});
factory Response1PageInfo.fromJson(Map<String, dynamic> json) {
return Response1PageInfo(
resultsPerPage: json['resultsPerPage'] as int,
totalResults: json['totalResults'] as int,
);
}
Map<String, dynamic> toJson() {
return {
'resultsPerPage': resultsPerPage,
'totalResults': totalResults,
};
}
}
String _fromDateTime(data) {
if (data == null) {
return null;
}
if (data is DateTime) {
return data.toIso8601String();
}
return data as String;
}
List _fromList(data, Function(dynamic) toJson) {
if (data == null) {
return null;
}
var result = [];
for (var element in data) {
var value;
if (element != null) {
value = toJson(element);
}
result.add(value);
}
return result;
}
DateTime _toDateTime(data) {
if (data == null) {
return null;
}
if (data is String) {
return DateTime.parse(data);
}
return data as DateTime;
}
T _toObject<T>(data, T Function(Map<String, dynamic>) fromJson) {
if (data == null) {
return null;
}
return fromJson(data as Map<String, dynamic>);
}
List<T> _toObjectList<T>(data, T Function(Map<String, dynamic>) fromJson) {
if (data == null) {
return null;
}
var result = <T>[];
for (var element in data) {
T value;
if (element != null) {
value = fromJson(element as Map<String, dynamic>);
}
result.add(value);
}
return result;
}
/*
Response1:
"kind": String
"etag": String
"nextPageToken": String
"regionCode": String
"pageInfo": Response1PageInfo
"items": List<Response1Items>
Response1PageInfo:
"totalResults": int
"resultsPerPage": int
Response1Items:
"kind": String
"etag": String
"id": Response1ItemsId
"snippet": Response1ItemsSnippet
Response1ItemsId:
"kind": String
"videoId": String
Response1ItemsSnippet:
"publishedAt": DateTime
"channelId": String
"title": String
"description": String
"thumbnails": Response1ItemsSnippetThumbnails
Response1ItemsSnippetThumbnails:
"default": Response1ItemsSnippetThumbnail
"medium": Response1ItemsSnippetThumbnail
Response1ItemsSnippetThumbnail:
"url": String
"width": int
"height": int
*/