2016-07-21 64 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