【问题标题】:jackson array deserilization to properties of singe object java杰克逊数组序列化到单个对象java的属性
【发布时间】:2016-07-21 22:00:34
【问题描述】:

我的类结构如下:

@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 元素的数组,但是当我将它们反序列化回来时,我想要如下结构:

公共类平面结构 {

   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 类的属性?

【问题讨论】:

  • @Abby,感谢您的回答,但是我并不想将 json 元素数组反序列化为 List。我正在尝试将 json 元素数组反序列化为另一个对象的属性。因此不重复。请再次阅读我的帖子。

标签: json jackson


【解决方案1】:

你可以添加一个带有注解@JsonCreator的构造函数

,在每个类中测试

还有一个扁平结构类, 之后,您使用 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 .....
}

应该与映射器一起正常工作

编辑:

Custom JSON Deserialization with Jackson

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

【讨论】:

  • 感谢您的回答,但我希望编写自定义反序列化器。有什么想法吗?
  • 检查我的编辑,我找到了两个关于自定义反序列化器的链接,我想这就是你想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-11
  • 2018-03-16
  • 2016-08-24
  • 2018-12-18
  • 1970-01-01
  • 2016-10-28
相关资源
最近更新 更多