我想在json后反序列化:
{
"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"
不起作用。
我找不到办法。
答案 0 :(得分:0)
Json解析:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String json = "{\"partnerName\":\"PartnerName\",\"mpnId\":\"1234567\",\"profileType\":\"partner_network_profile\",\"links\":{\"self\":{\"uri\":\"/v1/profiles/PartnerNetworkProfile?mpnId=1234567\",\"method\":\"GET\",\"headers\":[]}},\"attributes\":{\"objectType\":\"PartnerNetworkProfile\"}}";
ObjectNode rootNode = (ObjectNode)mapper.readTree(json);
JsonNode typeNode = rootNode.path("attributes");
if (JsonNodeType.OBJECT == typeNode.getNodeType()) {
System.out.println("Result:" + typeNode.get("objectType").asText());
}
自定义类:
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE, include = JsonTypeInfo.As.PROPERTY, property = "attributes.objectType")
@JsonSubTypes({
@JsonSubTypes.Type(value = ChildClass.class, name = "PartnerNetworkProfile")
})
class BaseClass {
@JsonProperty("links")
private Map<String, Link> links;
@JsonProperty("attributes")
private Attributes attributes;
public Map<String, Link> getLinks() {
return links;
}
public void setLinks(Map<String, Link> links) {
this.links = links;
}
public Attributes getAttributes() {
return attributes;
}
public void setAttributes(Attributes attributes) {
this.attributes = attributes;
}
}
class ChildClass extends BaseClass {
@JsonProperty("partnerName")
private String partnerName;
@JsonProperty("mpnId")
private String mpnId;
@JsonProperty("profileType")
private String profileType;
public String getPartnerName() {
return partnerName;
}
public void setPartnerName(String partnerName) {
this.partnerName = partnerName;
}
public String getMpnId() {
return mpnId;
}
public void setMpnId(String mpnId) {
this.mpnId = mpnId;
}
public String getProfileType() {
return profileType;
}
public void setProfileType(String profileType) {
this.profileType = profileType;
}
}
class Link {
private String uri;
private String method;
private String[] headers;
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String[] getHeaders() {
return headers;
}
public void setHeaders(String[] headers) {
this.headers = headers;
}
}
class Attributes {
@JsonProperty("objectType")
private String objectType;
public String getObjectType() {
return objectType;
}
public void setObjectType(String objectType) {
this.objectType = objectType;
}
}