定制漂亮的打印机使用Jackson库 [英] Custom pretty printer using Jackson library

查看:134
本文介绍了定制漂亮的打印机使用Jackson库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Map对象中有数据,我想以json格式打印它。我尝试使用DefaultPrettyPrinter

I have data in a Map object and I want to print it in a json format. I tried using DefaultPrettyPrinter

mapper.writerWithDefaultPrettyPrinter().writeValue(filePath, mapObject);

但格式不符合我的预期。我得到这样的输出:

but the format is not what I expected. I am getting output like this:

{
  "arrVals" : ["value-1","value-2"]
}

我想要这样的输出:

{
  "arrVals" : [
    "value-1",
    "value-2"
  ]
}


推荐答案

在Array之前需要缩进值。您可以使用 indentArraysWith 方法设置 Lf2SpacesIdenter 对象,该对象基本上会添加换行符后跟2个空格。
这可能会解决您的问题。

You need indentation before Array Values. You can use indentArraysWith method to set the Lf2SpacesIdenter object which will basically add a line feed followed by 2 spaces. This might solve your problem.

DefaultPrettyPrinter pp = new DefaultPrettyPrinter();
pp.indentArraysWith(new Lf2SpacesIndenter());
mapper.writer(pp).writeValue(filePath, mapObject);

这篇关于定制漂亮的打印机使用Jackson库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆