java实现JSON字符串格式化输出
一、JSON 字符串格式化输出
json字符串格式化输出fastjson、gson、jackson实现
import org.junit.jupiter.api.Test; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import lombok.extern.slf4j.Slf4j; /** * Json字符串格式化 */ @Slf4j public class JsonPrettyFormat { private 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\", \"编程语言\" ]}}"; @Test public void fastjsonTest() { JSONObject object = JSONObject.parseObject(jsonString); String pretty = JSON.toJSONString(object, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat); log.info("{} ", pretty); } @Test public void gsonTest() { JsonObject jsonObject = JsonParser.parseString(jsonString).getAsJsonObject(); Gson gson = new GsonBuilder().setPrettyPrinting().create(); String pretty = gson.toJson(jsonObject); log.info("{} ", pretty); } @Test public void jacksonTest() throws JsonMappingException, JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); Object obj = mapper.readValue(jsonString, Object.class); String pretty = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj); log.info("{} ", pretty); JsonNode jsonNode = mapper.readTree(jsonString); pretty = jsonNode.toPrettyString(); log.info("{} ", pretty); } }
二、运行结果
2023-12-19 17:26:19.704 INFO 21640 --- [ main] c.f.t.r.j.JsonPrettyFormat : {
"_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
}
2023-12-19 17:26:19.744 INFO 21640 --- [ main] c.f.t.r.j.JsonPrettyFormat : {
"_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",
"编程语言"
]
}
}
2023-12-19 17:26:19.991 INFO 21640 --- [ main] c.f.t.r.j.JsonPrettyFormat : {
"_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", "编程语言" ]
}
}
2023-12-19 17:26:19.999 INFO 21640 --- [ main] c.f.t.r.j.JsonPrettyFormat : {
"_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", "编程语言" ]
}
}
三、源码传送
到此这篇关于java实现JSON字符串格式化输出的文章就介绍到这了,更多相关java JSON字符串格式化输出内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
解决Lombok使用@Builder无法build父类属性的问题
这篇文章主要介绍了解决Lombok使用@Builder无法build父类属性的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-09-09HttpServletRequest对象简介_动力节点Java学院整理
这篇文章主要为大家详细介绍了HttpServletRequest对象简介的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-07-07
最新评论