【发布时间】: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 元素数组反序列化为另一个对象的属性。因此不重复。请再次阅读我的帖子。