如何获得精美打印的JSON代码的紧凑形式? [英] How do I get the compact form of pretty-printed JSON code?
问题描述
如何制作杰克逊的build()方法是否打印出它的JSON输出?这是一个漂亮打印丑陋形式的JSON代码的例子。我需要使用JSON代码的漂亮版本然后将其转移到丑陋的fom。怎么做到呢?
我需要兑换这个:
How do I make Jackson's build() method pretty-print its JSON output? Here is an example that pretty-prints the ugly form of JSON code. I need to take the nice version of JSON code then convet it to ugly fom. How can it be done? I need to convert this:
{
"one" : "AAA",
"two" : [ "BBB", "CCC" ],
"three" : {
"four" : "DDD",
"five" : [ "EEE", "FFF" ]
}
}
到此:
{"one":"AAA","two":["BBB","CCC"],"three":{"four":"DDD","five":["EEE","FFF"]}}
I试图删除'\ n','\t'和''字符;但是值中可能有一些这样的字符,所以我不能这样做。还有什么可以做?
I tried to remove '\n', '\t', and ' ' characters; but there may be some of these characters in values so I can't do that. What else can be done?
推荐答案
杰克逊允许你读取一个JSON字符串,所以读取漂亮的字符串回到杰克逊然后再次输出它并禁用漂亮打印。
Jackson allows you to read from a JSON string, so read the pretty-printed string back into Jackson and then output it again with pretty-print disabled.
简单示例
String prettyJsonString = "{ \"Hello\" : \"world\"}";
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readValue(prettyJsonString, JsonNode.class);
System.out.println(jsonNode.toString());
需要
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>
这篇关于如何获得精美打印的JSON代码的紧凑形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!