Jackson JSON 反序列化:每行中的数组元素 [英] Jackson JSON Deserialization: array elements in each line

查看:27
本文介绍了Jackson JSON 反序列化:每行中的数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Jackson 并且想要漂亮地打印 JSON,以便数组中的每个元素都转到每一行,喜欢:

<前>{"foo" : "酒吧",等等":[1、2、3]}

设置 SerializationFeature.INDENT_OUTPUT true 只会为对象字段插入换行符,而不是数组元素,以这种方式打印对象:

<前>{"foo" : "酒吧",等等":[1, 2, 3]}

有谁知道如何实现这一目标?谢谢!

解决方案

您可以扩展 DefaultPrettyPrinter 并覆盖方法 beforeArrayValues(…)writeArrayValueSeparator(…) 以存档所需的行为.之后,您必须通过 setPrettyPrinter(…).

I am using Jackson and would like to pretty-print JSON such that each element in arrays goes to each line, like:

{
  "foo" : "bar",
  "blah" : [
    1,
    2,
    3
  ]
}

Setting SerializationFeature.INDENT_OUTPUT true only inserts newline characters for object fields, not array elements, printing the object in this way instead:

{
  "foo" : "bar",
  "blah" : [1, 2, 3]
}

Does anyone know how to achieve this? Thanks!

解决方案

You could extend the DefaultPrettyPrinter and override the methods beforeArrayValues(…) and writeArrayValueSeparator(…) to archieve the desired behaviour. Afterwards you have to add your new Implementation to your JsonGenerator via setPrettyPrinter(…).

这篇关于Jackson JSON 反序列化:每行中的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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