2016-07-21 61 views
2

我有如下一類結構燒毛對象的java屬性:傑克遜陣列deserilization到

@JsonTypeInfo(使用= JsonTypeInfo.Id.NAME,包括= JsonTypeInfo.As.PROPERTY,屬性= 「類型」)

@JsonSubTypes({ 
      @JsonSubTypes.Type(name = "testA", value = TestA.class), 
      @JsonSubTypes.Type(name = "testB", value = TestB.class), 
      @JsonSubTypes.Type(name = "testC", value = TestC.class) 
    }) 
    public abstract class Test { 

    } 


    public class TestA extends Test { 
     private String firstName; 
     private String secondName; 
     private String surName; 
    } 

    public class TestB extends Test { 
     private String adressLine1; 
     private String adressLine2; 
     private String adressLine3; 
    } 


    public class TestC extends Test { 
     private String hobby1; 
     private String hobby2; 
     private String hobby3; 
    } 

上述類別被序列化爲JSON元件的陣列,但是當我去連載他們回來,我想如下結構:

公共類FlatStructure {

private TestA testA; 
    private TestB testB; 
    private TestC testC; 

    public void setTestA(TestA testA){ 
    this.testA = testA; 
} 

public TestA getTestA(){ 
    return testA; 
} 
.....getter and setter for testB and testC... 
} 

是否可以將testA,testB和testC類型的元素數組轉換爲FlatStructure類的屬性?

+1

[如何使用傑克遜反序列化對象數組]可能重複(http://stackoverflow.com/questions/6349421/how-to-use-jackson-to-deserialise-an-array-of-對象) –

+0

@Abby,謝謝你的回答,但是我不想將json元素的數組反序列化爲List。我試圖反序列化json元素的數組作爲另一個對象的屬性。因此不重複。請再次閱讀我的帖子。 – Moh

回答

0

您可以添加一個構造函數與註釋@JsonCreator

,在每類測試

,另外一個在你扁平結構類, 後您使用ObjectMapper創建類FlatStructure

我會建議使用annotaions @JsonProperty還對你的構造

檢查此鏈接

http://buraktas.com/convert-objects-to-from-json-by-jackson-example/

我認爲

public class TestA extends Test { 
    ..... 
    @JsonCreator 
    public TestA(@JsonProperty("firstName") String name, 
       @JsonProperty("secondName") String secondeName,     
       @JsonProperty("surName") String surName){ 
     this.firstName=name; 
     this.secondeName=secondeName; 
     this.surName=surName; 

    } 



    ... getter, setter ..... 
} 

    public class TestB extends Test { 
    ..... 
     @JsonCreator 
    public TestB(@JsonProperty("adressLine1") String adressLine1, 
       @JsonProperty("adressLine2") String adressLine2,     
       @JsonProperty("adressLine3") String adressLine3){ 
     this.adressLine1=adressLine1; 
     this.adressLine2=adressLine2; 
     this.adressLine3=adressLine3; 
    } 



    ... getter, setter ..... 
} 

    public class TestC extends Test { 
    ..... 
     @JsonCreator 
    public TestC(@JsonProperty("hobby1") String hobby1, 
       @JsonProperty("hobby2") String hobby2,     
       @JsonProperty("hobby3") String hobby3){ 
     this.hobby1=hobby1; 
     this.hobby2=hobby2; 
     this.hobby3=hobby3; 
    } 

    ... getter, setter ..... 
} 


public class FlatStructure{ 
     private TestA testA; 
     private TestB testB; 
     private TestC testC; 

     @JsonCreator 
     public FlatStructure(@JsonProperty("testA") testA testa, 
          @JsonProperty("testB") testB testb,     
          @JsonProperty("testC") testC testc){ 
     this.testA =testa; 
     this.testB =testb; 
     this.testC =testc; 
    } 


    ... getter, setter ..... 
} 

應該用映射

編輯正常工作:

Custom JSON Deserialization with Jackson

http://www.baeldung.com/jackson-deserialization

+0

感謝您的回答,但我期待編寫自定義反序列化器。有任何想法嗎? – Moh

+0

檢查我的編輯,我找到兩個關於自定義反序列化器的鏈接,我想這是你想要的。 – Koraxos