Java 格式化输出JSON字符串的2种实现操作
作者:瘦风的南墙 时间:2023-11-13 09:41:10
1 使用阿里的FastJson
1.1 项目的pom.xml依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
1.2 Java示例代码
(1) 导入的包:
com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
(2) 测试代码:
其中JSON字符串为:
{"_index":"book_shop","_type":"it_book","_id":"1","_score":1.0,"_source":{"name": "Java编程思想(第4版)","author": "[美] Bruce Eckel","category": "编程语言","price": 109.0,"publisher": "机械工业出版社","date": "2007-06-01","tags": [ "Java", "编程语言" ]}}
public static void main(String[] args) {
String jsonString = "{\"_index\":\"book_shop\",\"_type\":\"it_book\",\"_id\":\"1\",\"_score\":1.0," +
"\"_source\":{\"name\": \"Java编程思想(第4版)\",\"author\": \"[美] Bruce Eckel\",\"category\": \"编程语言\"," +
"\"price\": 109.0,\"publisher\": \"机械工业出版社\",\"date\": \"2007-06-01\",\"tags\": [ \"Java\", \"编程语言\" ]}}";
JSONObject object = JSONObject.parseObject(jsonString);
String pretty = JSON.toJSONString(object, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteDateUseDateFormat);
System.out.println(pretty);
}
(3) 格式化输出后的结果:
说明: FastJson通过Tab键进行换行后的格式化.
{
"_index":"book_shop",
"_type":"it_book",
"_source":{
"date":"2007-06-01",
"author":"[美] Bruce Eckel",
"price":109.0,
"name":"Java编程思想(第4版)",
"publisher":"机械工业出版社",
"category":"编程语言",
"tags":[
"Java",
"编程语言"
]
},
"_id":"1",
"_score":1.0
}
2 使用谷歌的Gson
2.1 项目的pom.xml依赖
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
2.2 Java示例代码
(1) 导入的包:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
(2) 测试代码:
JSON字符串与上述测试代码相同.
public static void main(String[] args) {
String jsonString = "{\"_index\":\"book_shop\",\"_type\":\"it_book\",\"_id\":\"1\",\"_score\":1.0," +
"\"_source\":{\"name\": \"Java编程思想(第4版)\",\"author\": \"[美] Bruce Eckel\",\"category\": \"编程语言\"," +
"\"price\": 109.0,\"publisher\": \"机械工业出版社\",\"date\": \"2007-06-01\",\"tags\": [ \"Java\", \"编程语言\" ]}}";
String pretty = toPrettyFormat(jsonString)
System.out.println(pretty);
}
/**
* 格式化输出JSON字符串
* @return 格式化后的JSON字符串
*/
private static String toPrettyFormat(String json) {
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(json).getAsJsonObject();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
return gson.toJson(jsonObject);
}
(3) 格式化输出后的结果:
说明: Gson使用2个空格作为换行后的格式转换.
{
"_index": "book_shop",
"_type": "it_book",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "Java编程思想(第4版)",
"author": "[美] Bruce Eckel",
"category": "编程语言",
"price": 109.0,
"publisher": "机械工业出版社",
"date": "2007-06-01",
"tags": [
"Java",
"编程语言"
]
}
}
来源:https://www.cnblogs.com/shoufeng/p/11196500.html
标签:Java,格式化,JSON,字符串
0
投稿
猜你喜欢
C# Socket编程实现简单的局域网聊天器的示例代码
2022-10-01 19:45:45
springboot使用自定义注解实现aop切面日志
2023-11-11 09:14:48
Java中i++的一些问题总结
2022-04-11 18:07:34
springboot集成shiro详细总结
2022-07-12 14:56:17
Maven项目修改JDK版本全过程
2021-07-19 12:13:29
使用java实现http多线程断点下载文件(二)
2021-06-08 04:26:17
WinForm项目开发中Excel用法实例解析
2022-07-11 10:23:03
SpringBoot+MQTT+apollo实现订阅发布功能的示例
2021-08-30 22:36:46
Java进阶必备之多线程编程
2023-12-17 04:24:51
Java加载资源文件时的路径问题的解决办法
2023-05-09 23:22:40
SpringBoot自动配置深入探究实现原理
2023-08-06 09:59:58
Java 线程池原理深入分析
2023-01-30 19:59:43
Mybatis实现自定义类型转换器TypeHandler的方法
2023-09-28 14:52:22
web压力测试工具_动力节点Java 学院整理
2022-03-25 00:48:21
c#实现爬虫程序
2023-04-19 18:59:14
RocketMQ源码解析topic创建机制详解
2023-03-16 01:06:23
IDEA修改idea64.exe.vmoptions文件以及解决coding卡顿问题
2022-11-02 00:36:04
Maven将代码及依赖打成一个Jar包的方式详解(最新推荐)
2022-03-31 06:52:47
Android实现底部导航栏功能(选项卡)
2022-01-08 21:34:14
Android PopupWindow 点击外面取消实现代码
2022-02-04 20:52:05