Java 扁平化FlexJSON输出
我有一个类,称之为Java 扁平化FlexJSON输出,java,flexjson,Java,Flexjson,我有一个类,称之为ClassOne,它有几个带有getter/setter的字段,其中一个是另一个类的对象,ClassTwo。我试图使用FlexJSON序列化ClassOne对象,如下所示: public class ClassOne{ private String name; private Long id; private ClassTwo classTwo; //Getters+Setters omitted for brevity } public cl
ClassOne
,它有几个带有getter/setter的字段,其中一个是另一个类的对象,ClassTwo
。我试图使用FlexJSON序列化ClassOne
对象,如下所示:
public class ClassOne{
private String name;
private Long id;
private ClassTwo classTwo;
//Getters+Setters omitted for brevity
}
public class ClassTwo{
private String description;
//Getter+Setter again omitted
}
//Elsewhere
List<ClassOne> list = /*get a list of ClassOne objects*/;
String json = new JSONSerializer().include("name", "id", "classTwo.description").exclude("*").serialize(list);
classTwo.description
部分被放入JSON中它自己单独的对象中。但class2本质上只是一个实现细节;我希望结果像这样平展:
[{"id":"1","name":"aName","classTwo":{"description":"aDescription"}},{"id":"2","name":"anotherName","classTwo":{"description":"anotherDescription"}}]
[{"id":"1","name":"aName","description":"aDescription"},{"id":"2","name":"anotherName","description":"anotherDescription"}]
如果“description”部分是“class2.description”,我也可以,只要它被展平为classOne
对象的表示
有没有办法用FlexJSON做到这一点?Transformer的东西看起来应该可以,但我对这个库太陌生了,不能确定,也不知道该怎么做