我在Map对象中有数据,我想以json格式打印它。我尝试使用DefaultPrettyPrinter
mapper.writerWithDefaultPrettyPrinter().writeValue(filePath, mapObject);
但格式不符合我的预期。我得到这样的输出:
{
"arrVals" : ["value-1","value-2"]
}
我想要这样的输出:
{
"arrVals" : [
"value-1",
"value-2"
]
}
答案 0 :(得分:16)
您需要在数组值之前缩进。您可以使用indentArraysWith
方法设置Lf2SpacesIdenter
对象,该对象基本上会添加换行符后跟2个空格。
这可能会解决您的问题。
DefaultPrettyPrinter pp = new DefaultPrettyPrinter();
pp.indentArraysWith(new Lf2SpacesIndenter());
mapper.writer(pp).writeValue(filePath, mapObject);