尝试反序列化 JSON 时出现以下异常
找不到适合类型的构造函数,
[simple type, class MyObj$obj$Card]:
无法从 JSON 对象实例化(需要添加/启用类型信息?)在[Source: java.io.StringReader@4344ee21; line: 1, column: 201]
(通过引用链MyObj["obj"]->Obj["cards"]
:)
JSON是
{
"obj":{
"api":"OK",
"cache":false,
"cards":[
{
"id":1232995897,
"items":[
{
"id":"vmdSJLpnY",
"cat":50,
"rating":0.0
}
]
},
{
"id":0005897,
"items":[
{
"id":"vxdSJLpnY",
"cat":50,
"rating":0.0
}
]
}
]
}
}
在Obj
课堂上我有以下陈述
@JsonProperty("cards") private Card[] cards;
这会产生上述异常。将类型更改Card[]
为Object[]
不会产生异常,但它缺少我想要获得的正确映射。
任何线索我该如何解决?一个片段会很棒!这个错误到底意味着什么?
更新
我已经包含了 Java 类。
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.Gson;
@JsonIgnoreProperties(ignoreUnknown=true)
public final class MyObj {
@JsonIgnoreProperties(ignoreUnknown=true)
public final class Obj {
@JsonIgnoreProperties(ignoreUnknown=true)
public final class Card {
@JsonIgnoreProperties(ignoreUnknown=true)
public final class Item {
@JsonProperty("id") private String id;
@JsonProperty("cat") private String cat;
@JsonProperty("rating") private String rating;
public final String getId() { return id; }
public final String getCat() { return cat; }
public final String getRating() { return ranting; }
public final String toString() { return new Gson().toJson(this); }
}
@JsonProperty("items") private Item[] items;
public final Item[] getItems() { return items; }
public final String toString() { return new Gson().toJson(this); }
}
@JsonProperty("cards") private Card[] cards;
public Card[] getCards() { return cards; }
public final String toString() { return new Gson().toJson(this); }
}
@JsonProperty("obj") MyObj obj;
public final Card[] getCards(){ return apiServiceResultsNoLists.getCards(); }
}