现在我有一个 org.fasterxml.jackson.databind.ObjectMapper
的实例,并希望得到一个带有漂亮 JSON 的 String
。我的 Google 搜索的所有结果都提出了 Jackson 1.x 执行此操作的方法,而我似乎无法找到正确的、未弃用的 2.2 执行此操作的方法。尽管我不认为代码对于这个问题是绝对必要的,但我现在拥有的是:
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);
System.out.println("\n\n----------REQUEST-----------");
StringWriter sw = new StringWriter();
mapper.writeValue(sw, jsonObject);
// Want pretty version of sw.toString() here
最佳答案
您可以通过在 ObjectMapper
上设置 SerializationFeature.INDENT_OUTPUT
来启用 pretty-print ,如下所示:
mapper.enable(SerializationFeature.INDENT_OUTPUT);
关于java - 来自 Jackson 2.2 的 ObjectMapper 的 pretty-print JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17617370/