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

问题描述

我正在使用,并且希望对JSON进行漂亮的打印,以使数组中的每个元素都指向每一行,像这样:

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
  ]
}

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

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!

推荐答案

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

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反序列化:每行中的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 10:30