如何用Java漂亮地打印已有的JSON数据?

时间:2022-01-26 02:23:22

I have a compact JSON string, and I want to format it nicely in Java without having to deserialize it first -- e.g. just like jsonlint.org does it. Are there any libraries out there that provides this?

我有一个紧凑的JSON字符串,我想要在Java中很好地格式化它,而不需要首先反序列化它——例如,就像jsonlint.org那样。有图书馆提供这个吗?

A similar solution for XML would also be nice.

类似的XML解决方案也不错。

7 个解决方案

#1


3  

I think for pretty-printing something, it's very helpful to know its structure.

我认为对于漂亮的印刷来说,了解它的结构是很有帮助的。

To get the structure you have to parse it. Because of this, I don't think it gets much easier than first parsing the JSON string you have and then using the pretty-printing method toString mentioned in the comments above.

要获得结构,必须对其进行解析。正因为如此,我不认为它比首先解析JSON字符串然后使用上面评论中提到的漂亮打印方法toString要容易得多。

Of course you can do similar with any JSON library you like.

当然,您可以对任何您喜欢的JSON库进行类似的操作。

#2


49  

int spacesToIndentEachLevel = 2;
new JSONObject(jsonString).toString(spacesToIndentEachLevel);

Using org.json.JSONObject (built in to JavaEE and Android)

使用org.json。JSONObject(内置到JavaEE和Android中)

#3


9  

In one line:

一行:

String niceFormattedJson = JsonWriter.formatJson(jsonString)

or

System.out.println(JsonWriter.formatJson(jsonString.toString()));

The json-io libray (https://github.com/jdereg/json-io) is a small (75K) library with no other dependencies than the JDK.

json-io libray是一个小型(75K)库,除了JDK之外没有其他依赖项。

In addition to pretty-printing JSON, you can serialize Java objects (entire Java object graphs with cycles) to JSON, as well as read them in.

除了漂亮地打印JSON之外,您还可以将Java对象(带有循环的整个Java对象图)序列化为JSON,并读取它们。

#4


5  

If you are using jackson you can easily achieve this with configuring a SerializationFeature in your ObjectMapper:

如果您正在使用jackson,您可以通过在ObjectMapper中配置SerializationFeature轻松实现这一点:

com.fasterxml.jackson.databind.ObjectMapper mapper = new ObjectMapper();

mapper.configure(SerializationFeature.INDENT_OUTPUT, true);

mapper.writeValueAsString(<yourObject>);

Thats it.

这是它。

#5


2  

Use gson. https://www.mkyong.com/java/how-to-enable-pretty-print-json-output-gson/

使用gson。https://www.mkyong.com/java/how-to-enable-pretty-print-json-output-gson/

Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(my_bean);

output

输出

{
  "name": "mkyong",
  "age": 35,
  "position": "Founder",
  "salary": 10000,
  "skills": [
    "java",
    "python",
    "shell"
  ]
}

#6


1  

I fount a very simple solution:

我发现了一个非常简单的解决方案:

<dependency>
    <groupId>com.cedarsoftware</groupId>
    <artifactId>json-io</artifactId>
    <version>4.5.0</version>
</dependency>

Java code:

Java代码:

import com.cedarsoftware.util.io.JsonWriter;
//...
String jsonString = "json_string_plain_text";
System.out.println(JsonWriter.formatJson(jsonString));

#7


0  

Another way to use gson:

另一种使用gson的方式:

String json_String_to_print = ...
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
return gson.toJson(jp.parse(json_String_to_print));

It can be used when you don't have the bean as in susemi99's post.

它可以在没有bean的情况下使用,如susemi99的文章所示。

#1


3  

I think for pretty-printing something, it's very helpful to know its structure.

我认为对于漂亮的印刷来说,了解它的结构是很有帮助的。

To get the structure you have to parse it. Because of this, I don't think it gets much easier than first parsing the JSON string you have and then using the pretty-printing method toString mentioned in the comments above.

要获得结构,必须对其进行解析。正因为如此,我不认为它比首先解析JSON字符串然后使用上面评论中提到的漂亮打印方法toString要容易得多。

Of course you can do similar with any JSON library you like.

当然,您可以对任何您喜欢的JSON库进行类似的操作。

#2


49  

int spacesToIndentEachLevel = 2;
new JSONObject(jsonString).toString(spacesToIndentEachLevel);

Using org.json.JSONObject (built in to JavaEE and Android)

使用org.json。JSONObject(内置到JavaEE和Android中)

#3


9  

In one line:

一行:

String niceFormattedJson = JsonWriter.formatJson(jsonString)

or

System.out.println(JsonWriter.formatJson(jsonString.toString()));

The json-io libray (https://github.com/jdereg/json-io) is a small (75K) library with no other dependencies than the JDK.

json-io libray是一个小型(75K)库,除了JDK之外没有其他依赖项。

In addition to pretty-printing JSON, you can serialize Java objects (entire Java object graphs with cycles) to JSON, as well as read them in.

除了漂亮地打印JSON之外,您还可以将Java对象(带有循环的整个Java对象图)序列化为JSON,并读取它们。

#4


5  

If you are using jackson you can easily achieve this with configuring a SerializationFeature in your ObjectMapper:

如果您正在使用jackson,您可以通过在ObjectMapper中配置SerializationFeature轻松实现这一点:

com.fasterxml.jackson.databind.ObjectMapper mapper = new ObjectMapper();

mapper.configure(SerializationFeature.INDENT_OUTPUT, true);

mapper.writeValueAsString(<yourObject>);

Thats it.

这是它。

#5


2  

Use gson. https://www.mkyong.com/java/how-to-enable-pretty-print-json-output-gson/

使用gson。https://www.mkyong.com/java/how-to-enable-pretty-print-json-output-gson/

Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(my_bean);

output

输出

{
  "name": "mkyong",
  "age": 35,
  "position": "Founder",
  "salary": 10000,
  "skills": [
    "java",
    "python",
    "shell"
  ]
}

#6


1  

I fount a very simple solution:

我发现了一个非常简单的解决方案:

<dependency>
    <groupId>com.cedarsoftware</groupId>
    <artifactId>json-io</artifactId>
    <version>4.5.0</version>
</dependency>

Java code:

Java代码:

import com.cedarsoftware.util.io.JsonWriter;
//...
String jsonString = "json_string_plain_text";
System.out.println(JsonWriter.formatJson(jsonString));

#7


0  

Another way to use gson:

另一种使用gson的方式:

String json_String_to_print = ...
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
return gson.toJson(jp.parse(json_String_to_print));

It can be used when you don't have the bean as in susemi99's post.

它可以在没有bean的情况下使用,如susemi99的文章所示。