java如何使用Jackson序列化对象的嵌套集合
具有以下代码:
public class ToSerialize {
List<NestedObject> objs;
public ToSerialize(List<NestedObject> objs) {
this.objs = objs;
}
}
public class NestedObject {
int intValue = 0;
String strValue = "Hello world";
}
如果可能,我如何设置Jackson,以获得以下CSV作为字符串输出:
obj1.intValue,obj1.strValue,obj2.intValue,obj2.strValue,0,"Hello world",0,"Hello world"
我想写我自己的自定义JsonSerializer
,但没办法。不过,这里是我当前无法工作的序列化程序实现
@Override
public void serialize(List<NestedObject> values, JsonGenerator gen, SerializerProvider serializers)
throws IOException
{
int i = 1;
for (Iterator<VsppAssetEntry> iterator = values.iterator(); iterator.hasNext(); ) {
VsppAssetEntry entry = iterator.next();
String fieldName = "obj" + i;
gen.writeObjectField(fieldName, entry);
i++;
}
这显然是抛出com.fasterxml.jackson.core.JsonGenerationException: Can not write a field name, expecting a value
,因为Jackson在输入此方法之前编写了一个字段名objs
# 1 楼答案
我建议Jackson dataformat CSV
考虑以下CSV模式:
和以下自定义序列化程序
…在
ToSerialize
类上注册它…它会给你