Json 格式化工具类 支持Jackson、FastJson、Gson
生活随笔
收集整理的這篇文章主要介紹了
Json 格式化工具类 支持Jackson、FastJson、Gson
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Json 格式化工具類
1、使用Jackson的DefaultPrettyPrinter來格式化json
/*** 將對象按以格式化json的方式寫出* 使用的json為Jackson** @param obj obj*/public static String formatJson(Object obj) throws JsonProcessingException {ObjectMapper mapper = new ObjectMapper();// 配置四個空格的縮進DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter(" ", DefaultIndenter.SYS_LF);DefaultPrettyPrinter printer = new DefaultPrettyPrinter();printer.indentObjectsWith(indenter); // Indent JSON objectsprinter.indentArraysWith(indenter); // Indent JSON arraysreturn mapper.writer(printer).writeValueAsString(obj);}2、使用Jackson的ObjectMapper的writerWithDefaultPrettyPrinter()來格式化json
/*** 輸出格式化的json字符串* 使用的json為Jackson** @param obj 任意對象* @return 格式化的json字符串* @throws JsonProcessingException jsonProcessingException*/public static String formatJson2(Object obj) throws JsonProcessingException {ObjectMapper mapper = new ObjectMapper();return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);}3、使用FastJson來格式化json
/*** 輸出格式化的json字符串* 使用的json為FastJson* <p>* 加上SerializerFeature.DisableCircularReferenceDetect關閉循環(huán)引用* 解決:* [* {* "playVideo":"觀看視頻",* "aboutVersion":"版本詳情"* },* {"$ref":"$[0]"},* {"$ref":"$[0]"},* {"$ref":"$[0]"}* ]** @param obj obj* @return 格式化的json字符串*/public static String formatJson3(Object obj) {// 輸出格式化后的字符串String pretty = JSON.toJSONString(obj, SerializerFeature.PrettyFormat,SerializerFeature.WriteDateUseDateFormat, SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullListAsEmpty, SerializerFeature.DisableCircularReferenceDetect);return pretty;}4、使用Gson來格式化json
/*** 輸出格式化的json字符串* 使用的json為Gson** @param obj obj* @return 格式化的json字符串*/public static String formatJson4(Object obj) {Gson gson = new GsonBuilder().setPrettyPrinting().create();return gson.toJson(obj);}5、完整代碼
package cn.lyf.utils;import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.util.DefaultIndenter; import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import lombok.extern.slf4j.Slf4j;/*** @author lyf* @description:* @version: v1.0* @since 2022-05-06 21:12*/ @Slf4j public final class JsonUtil {private JsonUtil() {}/*** 將對象按以格式化json的方式寫出* 使用的json為Jackson** @param obj obj*/public static String formatJson(Object obj) throws JsonProcessingException {ObjectMapper mapper = new ObjectMapper();// 配置四個空格的縮進DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter(" ", DefaultIndenter.SYS_LF);DefaultPrettyPrinter printer = new DefaultPrettyPrinter();printer.indentObjectsWith(indenter); // Indent JSON objectsprinter.indentArraysWith(indenter); // Indent JSON arraysreturn mapper.writer(printer).writeValueAsString(obj);}/*** 輸出格式化的json字符串* 使用的json為Jackson** @param obj 任意對象* @return 格式化的json字符串* @throws JsonProcessingException jsonProcessingException*/public static String formatJson2(Object obj) throws JsonProcessingException {ObjectMapper mapper = new ObjectMapper();return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);}/*** 輸出格式化的json字符串* 使用的json為FastJson* <p>* 加上SerializerFeature.DisableCircularReferenceDetect關閉循環(huán)引用* 解決:* [* {* "playVideo":"觀看視頻",* "aboutVersion":"版本詳情"* },* {"$ref":"$[0]"},* {"$ref":"$[0]"},* {"$ref":"$[0]"}* ]** @param obj obj* @return 格式化的json字符串*/public static String formatJson3(Object obj) {// 輸出格式化后的字符串String pretty = JSON.toJSONString(obj, SerializerFeature.PrettyFormat,SerializerFeature.WriteDateUseDateFormat, SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullListAsEmpty, SerializerFeature.DisableCircularReferenceDetect);return pretty;}/*** 輸出格式化的json字符串* 使用的json為Gson** @param obj obj* @return 格式化的json字符串*/public static String formatJson4(Object obj) {Gson gson = new GsonBuilder().setPrettyPrinting().create();return gson.toJson(obj);} }總結
以上是生活随笔為你收集整理的Json 格式化工具类 支持Jackson、FastJson、Gson的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Less学习--注释、变量、转义、可变插
- 下一篇: 照片、摄影处理中的基本知识