2016-03-04 57 views
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"不起作用。 我沒有找到辦法做到這一點。

回答

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; 
    } 
} 
+0

我有一個Java類,我婉它被映射。在這種情況下,你的解決方案不會有幫助。 –

+0

查看更新的答案。嘗試使用GSON –

+0

可以有多個子類,如ChildClass1,其中attributes.objectType ='ChildClass1',ChildClass2和attributes.objectType ='ChildClass2',不同子類的對象可以位於我想要的單個JSON數組中反序列化到列表對象,我將在其上檢查並檢查實例以獲取特定的ChildClass對象。所以,我們必須使用JsonSubTypes。 –