Как на DART распарсить сложный JSON?
Вот JSON https://pastebin.com/rYTPjPjH полученный с vk api wall.get
Знаю что надо создать модель, наверняка создал не правильно
class ItemModel {
int count;
List<Map<String, dynamic>> items;
ItemModel({
required this.count,
required this.items,
});
factory ItemModel.fromJson(Map<String, dynamic> parsedJson) {
return ItemModel(
count: parsedJson['count'],
items: parsedJson['items'],
);
}
}
Дальше делаю запрос через либу http
var response = await http.get(Uri.parse(url));
и вот что дальше с response.body делать не могу понять вообще хотел достать урлы картинок, но это наверно и сам смог если бы получить цикл всего items
Ответы (1 шт):
Автор решения: MiT
→ Ссылка
Модель через https://app.quicktype.io/ (но лучше изучите json_serializable):
class Vk {
final Response? response;
Vk({
this.response,
});
factory Vk.fromJson(Map<String, dynamic> json) => Vk(
response: json["response"] == null ? null : Response.fromJson(json["response"]),
);
Map<String, dynamic> toJson() => {
"response": response?.toJson(),
};
}
class Response {
final int? count;
final List<Item>? items;
Response({
this.count,
this.items,
});
factory Response.fromJson(Map<String, dynamic> json) => Response(
count: json["count"],
items: json["items"] == null ? [] : List<Item>.from(json["items"]!.map((x) => Item.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"count": count,
"items": items == null ? [] : List<dynamic>.from(items!.map((x) => x.toJson())),
};
}
class Item {
final int? id;
final int? fromId;
final int? ownerId;
final int? date;
final int? markedAsAds;
final bool? isFavorite;
final String? postType;
final String? text;
final int? isPinned;
final List<Attachment>? attachments;
final PostSource? postSource;
final Comments? comments;
final Likes? likes;
final Reposts? reposts;
final Views? views;
final Donut? donut;
final double? shortTextRate;
final String? hash;
Item({
this.id,
this.fromId,
this.ownerId,
this.date,
this.markedAsAds,
this.isFavorite,
this.postType,
this.text,
this.isPinned,
this.attachments,
this.postSource,
this.comments,
this.likes,
this.reposts,
this.views,
this.donut,
this.shortTextRate,
this.hash,
});
factory Item.fromJson(Map<String, dynamic> json) => Item(
id: json["id"],
fromId: json["from_id"],
ownerId: json["owner_id"],
date: json["date"],
markedAsAds: json["marked_as_ads"],
isFavorite: json["is_favorite"],
postType: json["post_type"],
text: json["text"],
isPinned: json["is_pinned"],
attachments: json["attachments"] == null ? [] :
List<Attachment>.from(json["attachments"]!.map((x) => Attachment.fromJson(x))),
postSource: json["post_source"] == null ? null : PostSource.fromJson(json["post_source"]),
comments: json["comments"] == null ? null : Comments.fromJson(json["comments"]),
likes: json["likes"] == null ? null : Likes.fromJson(json["likes"]),
reposts: json["reposts"] == null ? null : Reposts.fromJson(json["reposts"]),
views: json["views"] == null ? null : Views.fromJson(json["views"]),
donut: json["donut"] == null ? null : Donut.fromJson(json["donut"]),
shortTextRate: json["short_text_rate"]?.toDouble(),
hash: json["hash"],
);
Map<String, dynamic> toJson() => {
"id": id,
"from_id": fromId,
"owner_id": ownerId,
"date": date,
"marked_as_ads": markedAsAds,
"is_favorite": isFavorite,
"post_type": postType,
"text": text,
"is_pinned": isPinned,
"attachments": attachments == null ? [] : List<dynamic>.from(attachments!.map((x) => x.toJson())),
"post_source": postSource?.toJson(),
"comments": comments?.toJson(),
"likes": likes?.toJson(),
"reposts": reposts?.toJson(),
"views": views?.toJson(),
"donut": donut?.toJson(),
"short_text_rate": shortTextRate,
"hash": hash,
};
}
class Attachment {
final String? type;
final Video? video;
Attachment({
this.type,
this.video,
});
factory Attachment.fromJson(Map<String, dynamic> json) => Attachment(
type: json["type"],
video: json["video"] == null ? null : Video.fromJson(json["video"]),
);
Map<String, dynamic> toJson() => {
"type": type,
"video": video?.toJson(),
};
}
class Video {
final String? accessKey;
final int? canComment;
final int? canLike;
final int? canRepost;
final int? canSubscribe;
final int? canAddToFaves;
final int? canAdd;
final int? comments;
final int? date;
final String? description;
final int? duration;
final List<FirstFrame>? image;
final List<FirstFrame>? firstFrame;
final int? width;
final int? height;
final int? id;
final int? ownerId;
final String? title;
final bool? isFavorite;
final String? trackCode;
final int? repeat;
final String? type;
final int? views;
Video({
this.accessKey,
this.canComment,
this.canLike,
this.canRepost,
this.canSubscribe,
this.canAddToFaves,
this.canAdd,
this.comments,
this.date,
this.description,
this.duration,
this.image,
this.firstFrame,
this.width,
this.height,
this.id,
this.ownerId,
this.title,
this.isFavorite,
this.trackCode,
this.repeat,
this.type,
this.views,
});
factory Video.fromJson(Map<String, dynamic> json) => Video(
accessKey: json["access_key"],
canComment: json["can_comment"],
canLike: json["can_like"],
canRepost: json["can_repost"],
canSubscribe: json["can_subscribe"],
canAddToFaves: json["can_add_to_faves"],
canAdd: json["can_add"],
comments: json["comments"],
date: json["date"],
description: json["description"],
duration: json["duration"],
image: json["image"] == null ? [] :
List<FirstFrame>.from(json["image"]!.map((x) => FirstFrame.fromJson(x))),
firstFrame: json["first_frame"] == null ? [] :
List<FirstFrame>.from(json["first_frame"]!.map((x) => FirstFrame.fromJson(x))),
width: json["width"],
height: json["height"],
id: json["id"],
ownerId: json["owner_id"],
title: json["title"],
isFavorite: json["is_favorite"],
trackCode: json["track_code"],
repeat: json["repeat"],
type: json["type"],
views: json["views"],
);
Map<String, dynamic> toJson() => {
"access_key": accessKey,
"can_comment": canComment,
"can_like": canLike,
"can_repost": canRepost,
"can_subscribe": canSubscribe,
"can_add_to_faves": canAddToFaves,
"can_add": canAdd,
"comments": comments,
"date": date,
"description": description,
"duration": duration,
"image": image == null ? [] : List<dynamic>.from(image!.map((x) => x.toJson())),
"first_frame": firstFrame == null ? [] : List<dynamic>.from(firstFrame!.map((x) => x.toJson())),
"width": width,
"height": height,
"id": id,
"owner_id": ownerId,
"title": title,
"is_favorite": isFavorite,
"track_code": trackCode,
"repeat": repeat,
"type": type,
"views": views,
};
}
class FirstFrame {
final String? url;
final int? width;
final int? height;
final int? withPadding;
FirstFrame({
this.url,
this.width,
this.height,
this.withPadding,
});
factory FirstFrame.fromJson(Map<String, dynamic> json) => FirstFrame(
url: json["url"],
width: json["width"],
height: json["height"],
withPadding: json["with_padding"],
);
Map<String, dynamic> toJson() => {
"url": url,
"width": width,
"height": height,
"with_padding": withPadding,
};
}
class Comments {
final int? canPost;
final int? count;
final bool? groupsCanPost;
Comments({
this.canPost,
this.count,
this.groupsCanPost,
});
factory Comments.fromJson(Map<String, dynamic> json) => Comments(
canPost: json["can_post"],
count: json["count"],
groupsCanPost: json["groups_can_post"],
);
Map<String, dynamic> toJson() => {
"can_post": canPost,
"count": count,
"groups_can_post": groupsCanPost,
};
}
class Donut {
final bool? isDonut;
Donut({
this.isDonut,
});
factory Donut.fromJson(Map<String, dynamic> json) => Donut(
isDonut: json["is_donut"],
);
Map<String, dynamic> toJson() => {
"is_donut": isDonut,
};
}
class Likes {
final int? canLike;
final int? count;
final int? userLikes;
final int? canPublish;
Likes({
this.canLike,
this.count,
this.userLikes,
this.canPublish,
});
factory Likes.fromJson(Map<String, dynamic> json) => Likes(
canLike: json["can_like"],
count: json["count"],
userLikes: json["user_likes"],
canPublish: json["can_publish"],
);
Map<String, dynamic> toJson() => {
"can_like": canLike,
"count": count,
"user_likes": userLikes,
"can_publish": canPublish,
};
}
class PostSource {
final String? type;
PostSource({
this.type,
});
factory PostSource.fromJson(Map<String, dynamic> json) => PostSource(
type: json["type"],
);
Map<String, dynamic> toJson() => {
"type": type,
};
}
class Reposts {
final int? count;
final int? userReposted;
Reposts({
this.count,
this.userReposted,
});
factory Reposts.fromJson(Map<String, dynamic> json) => Reposts(
count: json["count"],
userReposted: json["user_reposted"],
);
Map<String, dynamic> toJson() => {
"count": count,
"user_reposted": userReposted,
};
}
class Views {
final int? count;
Views({
this.count,
});
factory Views.fromJson(Map<String, dynamic> json) => Views(
count: json["count"],
);
Map<String, dynamic> toJson() => {
"count": count,
};
}
Используем:
var response = await http.get(Uri.parse(url));
fianl vk = Vk.fromJson(json.decode(response.body);
final items = vk?.response?.items;