1
屬性多態類型欲反序列化以下JSON:使用傑克遜2.5.4罐子反序列含有的屬性
{
"partnerName" : "PartnerName",
"mpnId" : "1234567",
"profileType" : "partner_network_profile",
"links" : {
"self" : {
"uri" : "/v1/profiles/PartnerNetworkProfile?mpnId=1234567",
"method" : "GET",
"headers" : []
}
},
"attributes" : {
"objectType" : "PartnerNetworkProfile"
}
}
類型信息被包括在內屬性對象即attributes.objectType
類映射是這樣的:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "attributes.objectType", visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = ChildClass.class, name = "PartnerNetworkProfile")
})
BaseClass{
@JsonProperty("links")
private Map<String, Link> links;
@JsonProperty("attributes")
private Attributes attributes;
//getter & setters
}
ChildClass extends BaseClass {
@JsonProperty("partnerName")
private String partnerName;
@JsonProperty("mpnId")
private String mpnId;
@JsonProperty("profileType")
private String profileType;
//Getter & setters
}
public class Attributes {
private String objectType;
}
但property = "attributes.objectType"
不起作用。 我沒有找到辦法做到這一點。
我有一個Java類,我婉它被映射。在這種情況下,你的解決方案不會有幫助。 –
查看更新的答案。嘗試使用GSON –
可以有多個子類,如ChildClass1,其中attributes.objectType ='ChildClass1',ChildClass2和attributes.objectType ='ChildClass2',不同子類的對象可以位於我想要的單個JSON數組中反序列化到列表對象,我將在其上檢查並檢查實例以獲取特定的ChildClass對象。所以,我們必須使用JsonSubTypes。 –