jackson array deserilization to singe object java

时间:2016-07-21 22:00:34

标签: json jackson

我有一个类结构如下:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,include = JsonTypeInfo.As.PROPERTY,property =“type”)

    @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 个答案:

答案 0 :(得分:0)

您可以使用注释@JsonCreator

添加构造函数

,在每个类Test

和另一个平面结构类, 之后,您使用ObjectMapper创建您的类FlatStructure

我建议在构造函数

上使用注释@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 .....
}

应与mapper一起正常使用

修改

Custom JSON Deserialization with Jackson

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