无法使用Gson或Jackson反序列化类

hec6srdp  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(167)

我尝试将json字符串反序列化为Card.class,但无法执行此操作。

Card card =new Card();
    card.setHeader(new CardHeader().setTitle("HelloWorld"));
    Section section=new Section().setWidgets(Collections.singletonList(new WidgetMarkup().setImage(new Image().setImageUrl("https://gazabb//..com"))));
    card.setSections(Collections.singletonList(section));
    String req=GsonUtils.GSON.toJson(card,Card.class);
    System.out.println(req);

输出符合预期

{"header":{"title":"HelloWorld"},"sections":[{"widgets":[{"image":{"imageUrl":"https://gazabb//..com"}}]}]}

但当我试图将其序列化为Card.class(即

GsonUtils.GSON.fromJson(req,Card.class);

这是给我的错误:

java.lang.IllegalArgumentException: Can not set com.google.api.services.chat.v1.model.CardHeader field com.google.api.services.chat.v1.model.Card.header to com.google.gson.internal.LinkedTreeMap
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at com.google.api.client.util.FieldInfo.setFieldValue(FieldInfo.java:275)
at com.google.api.client.util.FieldInfo.setValue(FieldInfo.java:231)
at com.google.api.client.util.GenericData.put(GenericData.java:98)
at com.google.api.client.util.GenericData.put(GenericData.java:43)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:188)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145)
at io.gsonfire.gson.HooksTypeAdapter.deserialize(HooksTypeAdapter.java:86)
at io.gsonfire.gson.HooksTypeAdapter.read(HooksTypeAdapter.java:54)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.google.gson.Gson.fromJson(Gson.java:813)

卡片类为:

package com.google.api.services.chat.v1.model;
import com.google.api.client.json.GenericJson;
import com.google.api.client.util.Data;
import com.google.api.client.util.Key;
import java.util.List;

public final class Card extends GenericJson {
@Key
private List<CardAction> cardActions;
@Key
private CardHeader header;
@Key
private String name;
@Key
private List<Section> sections;

public Card() {
}

public List<CardAction> getCardActions() {
    return this.cardActions;
}

public Card setCardActions(List<CardAction> var1) {
    this.cardActions = var1;
    return this;
}

public CardHeader getHeader() {
    return this.header;
}

public Card setHeader(CardHeader var1) {
    this.header = var1;
    return this;
}

public String getName() {
    return this.name;
}

public Card setName(String var1) {
    this.name = var1;
    return this;
}

public List<Section> getSections() {
    return this.sections;
}

public Card setSections(List<Section> var1) {
    this.sections = var1;
    return this;
}

public Card set(String var1, Object var2) {
    return (Card)super.set(var1, var2);
}

public Card clone() {
    return (Card)super.clone();
}

static {
    Data.nullOf(CardAction.class);
    Data.nullOf(Section.class);
}

CardHeader类别:

package com.google.api.services.chat.v1.model;

import com.google.api.client.json.GenericJson;
import com.google.api.client.util.Key;

public final class CardHeader extends GenericJson {
@Key
private String imageStyle;
@Key
private String imageUrl;
@Key
private String subtitle;
@Key
private String title;

public CardHeader() {
}

public String getImageStyle() {
    return this.imageStyle;
}

public CardHeader setImageStyle(String var1) {
    this.imageStyle = var1;
    return this;
}

public String getImageUrl() {
    return this.imageUrl;
}

public CardHeader setImageUrl(String var1) {
    this.imageUrl = var1;
    return this;
}

public String getSubtitle() {
    return this.subtitle;
}

public CardHeader setSubtitle(String var1) {
    this.subtitle = var1;
    return this;
}

public String getTitle() {
    return this.title;
}

public CardHeader setTitle(String var1) {
    this.title = var1;
    return this;
}

public CardHeader set(String var1, Object var2) {
    return (CardHeader)super.set(var1, var2);
}

public CardHeader clone() {
    return (CardHeader)super.clone();
}

}
我已经用了两天了。从错误中可以清楚地看到,Card的CardHeader字段无法设置为LinkedTreeMap。但我无法找到原因。是否可以在不编写自己的TypeAdaptor的情况下反序列化它。我也尝试过用Jackson做同样的事情,但面临同样的错误。

dy2hfwbg

dy2hfwbg1#

在超类型中是否有一个头字段与您的字段冲突。删除"header":{"title":"HelloWorld"},错误消失。

相关问题