Распарсить JSON с помощью GSON VK API?
Пытаюсь распарсить JSON полученный из VK API. Взял WallPost.JAVA с SDK-API-VK, вроде собрал все правильно, учел что получу коллекцию, но все равно получаю ошибку. Скажите пожалуйста мою ошибку?
package org.example;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.nio.channels.Channel;
import java.util.Collection;
public class Main {
public static void main(String[] args) {
String json = "[{\"donut\":{\"is_donut\":false},\"comments\":{\"can_post\":\"1\",\"count\":0,\"groups_can_post\":true},\"marked_as_ads\":\"0\",\"short_text_rate\":0.8,\"hash\":\"hxEvo_ra4JArArSlMqnTzhGFvCB5\",\"attachments\":[{\"type\":\"video\",\"video\":{\"access_key\":\"7f7ce9151e8f44075a\",\"can_comment\":\"1\",\"can_like\":\"1\",\"can_repost\":\"1\",\"can_subscribe\":\"1\",\"can_add_to_faves\":\"1\",\"can_add\":\"1\",\"comments\":0,\"date\":1660482918,\"description\":\"\",\"duration\":6,\"image\":[{\"with_padding\":\"1\",\"height\":96,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2687969659398\\u0026idx\\u003d5\\u0026type\\u003d39\\u0026tkn\\u003dA8tRt3vN9IzVKojj243ZqbnkojM\\u0026fn\\u003dvid_s\",\"width\":130},{\"with_padding\":\"1\",\"height\":120,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2687969659398\\u0026idx\\u003d5\\u0026type\\u003d39\\u0026tkn\\u003dA8tRt3vN9IzVKojj243ZqbnkojM\\u0026fn\\u003dvid_m\",\"width\":160},{\"with_padding\":\"1\",\"height\":240,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2687969659398\\u0026idx\\u003d5\\u0026type\\u003d39\\u0026tkn\\u003dA8tRt3vN9IzVKojj243ZqbnkojM\\u0026fn\\u003dvid_l\",\"width\":320},{\"with_padding\":\"1\",\"height\":450,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2687969659398\\u0026idx\\u003d5\\u0026type\\u003d39\\u0026tkn\\u003dA8tRt3vN9IzVKojj243ZqbnkojM\\u0026fn\\u003dvid_x\",\"width\":800},{\"height\":720,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2687969659398\\u0026idx\\u003d5\\u0026type\\u003d39\\u0026tkn\\u003dA8tRt3vN9IzVKojj243ZqbnkojM\\u0026fn\\u003dvid_w\",\"width\":408},{\"height\":565,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2687969659398\\u0026idx\\u003d5\\u0026type\\u003d39\\u0026tkn\\u003dA8tRt3vN9IzVKojj243ZqbnkojM\\u0026fn\\u003dvid_t\",\"width\":320},{\"height\":1272,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2687969659398\\u0026idx\\u003d5\\u0026type\\u003d39\\u0026tkn\\u003dA8tRt3vN9IzVKojj243ZqbnkojM\\u0026fn\\u003dvid_u\",\"width\":720}],\"first_frame\":[{\"height\":720,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2687969659398\\u0026idx\\u003d0\\u0026type\\u003d39\\u0026tkn\\u003diKfjIprViPtFD9wFAZ-IFVJg7aM\\u0026fn\\u003dvid_f\",\"width\":408},{\"height\":480,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2687969659398\\u0026idx\\u003d0\\u0026type\\u003d39\\u0026tkn\\u003diKfjIprViPtFD9wFAZ-IFVJg7aM\\u0026fn\\u003dvid_md\",\"width\":272},{\"height\":240,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2687969659398\\u0026idx\\u003d0\\u0026type\\u003d39\\u0026tkn\\u003diKfjIprViPtFD9wFAZ-IFVJg7aM\\u0026fn\\u003dvid_d\",\"width\":136},{\"height\":128,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2687969659398\\u0026idx\\u003d0\\u0026type\\u003d39\\u0026tkn\\u003diKfjIprViPtFD9wFAZ-IFVJg7aM\\u0026fn\\u003dvid_sm\",\"width\":72},{\"height\":1280,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2687969659398\\u0026idx\\u003d0\\u0026type\\u003d39\\u0026tkn\\u003diKfjIprViPtFD9wFAZ-IFVJg7aM\",\"width\":725}],\"width\":480,\"height\":848,\"id\":456295504,\"owner_id\":-26493942,\"title\":\"����� � ��������� ����\",\"is_favorite\":false,\"track_code\":\"video_53a77eb2j0FUHM9pxI3Ap4DkEawq1Z8Xo61UR8e7HopAV6pN8Ey1YUAaywLIicmlhY8UmRznpiKWnWBDsNpy5kBG\",\"type\":\"video\",\"views\":211}},{\"type\":\"video\",\"video\":{\"access_key\":\"e18675c4ff3f29bb2a\",\"can_comment\":\"1\",\"can_like\":\"1\",\"can_repost\":\"1\",\"can_subscribe\":\"1\",\"can_add_to_faves\":\"1\",\"can_add\":\"1\",\"comments\":0,\"date\":1660482919,\"description\":\"\",\"duration\":10,\"image\":[{\"with_padding\":\"1\",\"height\":96,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2770360535610\\u0026idx\\u003d0\\u0026type\\u003d39\\u0026tkn\\u003dliqhsf6EiBpZ_XTvKXxrByOTEOQ\\u0026fn\\u003dvid_s\",\"width\":130},{\"with_padding\":\"1\",\"height\":120,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2770360535610\\u0026idx\\u003d0\\u0026type\\u003d39\\u0026tkn\\u003dliqhsf6EiBpZ_XTvKXxrByOTEOQ\\u0026fn\\u003dvid_m\",\"width\":160},{\"with_padding\":\"1\",\"height\":240,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2770360535610\\u0026idx\\u003d0\\u0026type\\u003d39\\u0026tkn\\u003dliqhsf6EiBpZ_XTvKXxrByOTEOQ\\u0026fn\\u003dvid_l\",\"width\":320},{\"with_padding\":\"1\",\"height\":450,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2770360535610\\u0026idx\\u003d0\\u0026type\\u003d39\\u0026tkn\\u003dliqhsf6EiBpZ_XTvKXxrByOTEOQ\\u0026fn\\u003dvid_x\",\"width\":800},{\"height\":720,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2770360535610\\u0026idx\\u003d0\\u0026type\\u003d39\\u0026tkn\\u003dliqhsf6EiBpZ_XTvKXxrByOTEOQ\\u0026fn\\u003dvid_w\",\"width\":1272},{\"height\":181,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2770360535610\\u0026idx\\u003d0\\u0026type\\u003d39\\u0026tkn\\u003dliqhsf6EiBpZ_XTvKXxrByOTEOQ\\u0026fn\\u003dvid_t\",\"width\":320},{\"height\":408,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2770360535610\\u0026idx\\u003d0\\u0026type\\u003d39\\u0026tkn\\u003dliqhsf6EiBpZ_XTvKXxrByOTEOQ\\u0026fn\\u003dvid_u\",\"width\":720}],\"first_frame\":[{\"height\":408,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2770360535610\\u0026idx\\u003d0\\u0026type\\u003d39\\u0026tkn\\u003dliqhsf6EiBpZ_XTvKXxrByOTEOQ\\u0026fn\\u003dvid_f\",\"width\":720},{\"height\":272,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2770360535610\\u0026idx\\u003d0\\u0026type\\u003d39\\u0026tkn\\u003dliqhsf6EiBpZ_XTvKXxrByOTEOQ\\u0026fn\\u003dvid_md\",\"width\":480},{\"height\":136,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2770360535610\\u0026idx\\u003d0\\u0026type\\u003d39\\u0026tkn\\u003dliqhsf6EiBpZ_XTvKXxrByOTEOQ\\u0026fn\\u003dvid_d\",\"width\":240},{\"height\":72,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2770360535610\\u0026idx\\u003d0\\u0026type\\u003d39\\u0026tkn\\u003dliqhsf6EiBpZ_XTvKXxrByOTEOQ\\u0026fn\\u003dvid_sm\",\"width\":128},{\"height\":725,\"url\":\"https://i.mycdn.me/getVideoPreview?id\\u003d2770360535610\\u0026idx\\u003d0\\u0026type\\u003d39\\u0026tkn\\u003dliqhsf6EiBpZ_XTvKXxrByOTEOQ\",\"width\":1280}],\"width\":848,\"height\":480,\"id\":456295505,\"owner_id\":-26493942,\"title\":\"�������� �������� ����� � ����\",\"is_favorite\":false,\"track_code\":\"video_b3316619Vrnv8l03IPX2e1ZV3LOmvtU8h9--tLRG9GcFDC2sXyZsmfv0WVws8f95Uz7ZhpCM7Amy74uwwyeYCwUd\",\"type\":\"video\",\"views\":106}}],\"date\":1660482920,\"from_id\":-26493942,\"id\":5706741,\"is_favorite\":false,\"likes\":{\"can_like\":\"1\",\"can_publish\":\"1\",\"count\":7,\"user_likes\":0},\"owner_id\":-26493942,\"post_source\":{\"type\":\"api\"},\"post_type\":\"post\",\"reposts\":{\"count\":1,\"user_reposted\":0},\"text\":\"� ���������� ������ ���� ���������� ���� \\\"�����\\\". �� ������ ������������ 5 �������. \\n\\n������ �� �����, �������� ����������, ��� ����� ������ �� ������� 100 ��. �. �� ������ ������ ���������� ��������� �������������. � ������� ������ ������������ 16 ������� � 5 ������ �������.\",\"views\":{\"count\":6058}}]\n";
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
WallPost wallPost = gson.fromJson(json, WallPost.class);
Type collectionType = new TypeToken<Collection<WallPost>>(){}.getType();
Collection<WallPost> enums = gson.fromJson(json, collectionType);
System.out.println(enums);
}
}
package org.example;
// Autogenerated from vk-api-schema. Please don't edit it manually.
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;
import com.vk.api.sdk.objects.Validable;
import com.vk.api.sdk.objects.base.Geo;
import com.vk.api.sdk.objects.base.LikesInfo;
import com.vk.api.sdk.objects.base.RepostsInfo;
import com.vk.api.sdk.objects.wall.*;
import java.util.List;
import java.util.Objects;
/**
* Wallpost object
*/
public class WallPost implements Validable {
/**
* Access key to private object
*/
@SerializedName("access_key")
private String accessKey;
@SerializedName("is_deleted")
private Boolean isDeleted;
@SerializedName("attachments")
private List<WallpostAttachment> attachments;
/**
* Information about the source of the post
*/
@SerializedName("copyright")
private PostCopyright copyright;
/**
* Date of publishing in Unixtime
*/
@SerializedName("date")
private Integer date;
/**
* Date of editing in Unixtime
*/
@SerializedName("edited")
private Integer edited;
/**
* Post author ID
*/
@SerializedName("from_id")
private Integer fromId;
@SerializedName("geo")
private Geo geo;
/**
* Post ID
*/
@SerializedName("id")
private Integer id;
/**
* Is post archived, only for post owners
*/
@SerializedName("is_archived")
private Boolean isArchived;
/**
* Information whether the post in favorites list
*/
@SerializedName("is_favorite")
private Boolean isFavorite;
/**
* Count of likes
*/
@SerializedName("likes")
private LikesInfo likes;
/**
* Wall owner's ID
*/
@SerializedName("owner_id")
private Integer ownerId;
@SerializedName("poster")
private JsonObject poster;
/**
* If post type 'reply', contains original post ID
*/
@SerializedName("post_id")
private Integer postId;
/**
* If post type 'reply', contains original parent IDs stack
*/
@SerializedName("parents_stack")
private List<Integer> parentsStack;
@SerializedName("post_source")
private PostSource postSource;
@SerializedName("post_type")
private PostType postType;
@SerializedName("reposts")
private RepostsInfo reposts;
/**
* Post signer ID
*/
@SerializedName("signer_id")
private Integer signerId;
/**
* Post text
*/
@SerializedName("text")
private String text;
/**
* Count of views
*/
@SerializedName("views")
private Views views;
public String getAccessKey() {
return accessKey;
}
public WallPost setAccessKey(String accessKey) {
this.accessKey = accessKey;
return this;
}
public Boolean getIsDeleted() {
return isDeleted;
}
public WallPost setIsDeleted(Boolean isDeleted) {
this.isDeleted = isDeleted;
return this;
}
public List<WallpostAttachment> getAttachments() {
return attachments;
}
public WallPost setAttachments(List<WallpostAttachment> attachments) {
this.attachments = attachments;
return this;
}
public PostCopyright getCopyright() {
return copyright;
}
public WallPost setCopyright(PostCopyright copyright) {
this.copyright = copyright;
return this;
}
public Integer getDate() {
return date;
}
public WallPost setDate(Integer date) {
this.date = date;
return this;
}
public Integer getEdited() {
return edited;
}
public WallPost setEdited(Integer edited) {
this.edited = edited;
return this;
}
public Integer getFromId() {
return fromId;
}
public WallPost setFromId(Integer fromId) {
this.fromId = fromId;
return this;
}
public Geo getGeo() {
return geo;
}
public WallPost setGeo(Geo geo) {
this.geo = geo;
return this;
}
public Integer getId() {
return id;
}
public WallPost setId(Integer id) {
this.id = id;
return this;
}
public Boolean getIsArchived() {
return isArchived;
}
public WallPost setIsArchived(Boolean isArchived) {
this.isArchived = isArchived;
return this;
}
public Boolean getIsFavorite() {
return isFavorite;
}
public WallPost setIsFavorite(Boolean isFavorite) {
this.isFavorite = isFavorite;
return this;
}
public LikesInfo getLikes() {
return likes;
}
public WallPost setLikes(LikesInfo likes) {
this.likes = likes;
return this;
}
public Integer getOwnerId() {
return ownerId;
}
public WallPost setOwnerId(Integer ownerId) {
this.ownerId = ownerId;
return this;
}
public JsonObject getPoster() {
return poster;
}
public WallPost setPoster(JsonObject poster) {
this.poster = poster;
return this;
}
public Integer getPostId() {
return postId;
}
public WallPost setPostId(Integer postId) {
this.postId = postId;
return this;
}
public List<Integer> getParentsStack() {
return parentsStack;
}
public WallPost setParentsStack(List<Integer> parentsStack) {
this.parentsStack = parentsStack;
return this;
}
public PostSource getPostSource() {
return postSource;
}
public WallPost setPostSource(PostSource postSource) {
this.postSource = postSource;
return this;
}
public PostType getPostType() {
return postType;
}
public WallPost setPostType(PostType postType) {
this.postType = postType;
return this;
}
public RepostsInfo getReposts() {
return reposts;
}
public WallPost setReposts(RepostsInfo reposts) {
this.reposts = reposts;
return this;
}
public Integer getSignerId() {
return signerId;
}
public WallPost setSignerId(Integer signerId) {
this.signerId = signerId;
return this;
}
public String getText() {
return text;
}
public WallPost setText(String text) {
this.text = text;
return this;
}
public Views getViews() {
return views;
}
public WallPost setViews(Views views) {
this.views = views;
return this;
}
@Override
public int hashCode() {
return Objects.hash(date, copyright, attachments, edited, postType, isArchived, postSource, postId, ownerId, fromId, geo, parentsStack, signerId, isDeleted, accessKey, id, text, poster, reposts, views, isFavorite, likes);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
WallPost wallpost = (WallPost) o;
return Objects.equals(date, wallpost.date) &&
Objects.equals(copyright, wallpost.copyright) &&
Objects.equals(attachments, wallpost.attachments) &&
Objects.equals(fromId, wallpost.fromId) &&
Objects.equals(isFavorite, wallpost.isFavorite) &&
Objects.equals(signerId, wallpost.signerId) &&
Objects.equals(edited, wallpost.edited) &&
Objects.equals(parentsStack, wallpost.parentsStack) &&
Objects.equals(ownerId, wallpost.ownerId) &&
Objects.equals(geo, wallpost.geo) &&
Objects.equals(isDeleted, wallpost.isDeleted) &&
Objects.equals(isArchived, wallpost.isArchived) &&
Objects.equals(postId, wallpost.postId) &&
Objects.equals(postSource, wallpost.postSource) &&
Objects.equals(accessKey, wallpost.accessKey) &&
Objects.equals(postType, wallpost.postType) &&
Objects.equals(id, wallpost.id) &&
Objects.equals(text, wallpost.text) &&
Objects.equals(poster, wallpost.poster) &&
Objects.equals(reposts, wallpost.reposts) &&
Objects.equals(views, wallpost.views) &&
Objects.equals(likes, wallpost.likes);
}
@Override
public String toString() {
final Gson gson = new Gson();
return gson.toJson(this);
}
public String toPrettyString() {
final StringBuilder sb = new StringBuilder("Wallpost{");
sb.append("date=").append(date);
sb.append(", copyright=").append(copyright);
sb.append(", attachments=").append(attachments);
sb.append(", fromId=").append(fromId);
sb.append(", isFavorite=").append(isFavorite);
sb.append(", signerId=").append(signerId);
sb.append(", edited=").append(edited);
sb.append(", parentsStack=").append(parentsStack);
sb.append(", ownerId=").append(ownerId);
sb.append(", geo=").append(geo);
sb.append(", isDeleted=").append(isDeleted);
sb.append(", isArchived=").append(isArchived);
sb.append(", postId=").append(postId);
sb.append(", postSource=").append(postSource);
sb.append(", accessKey='").append(accessKey).append("'");
sb.append(", postType=").append(postType);
sb.append(", id=").append(id);
sb.append(", text='").append(text).append("'");
sb.append(", poster=").append(poster);
sb.append(", reposts=").append(reposts);
sb.append(", views=").append(views);
sb.append(", likes=").append(likes);
sb.append('}');
return sb.toString();
}
}
ларчик как оказалось просто открывался. Конструкция get(0), решает проблему. GSON не нужен.
System.out.println(getResponse.getItems().get(0).getText());
