【随笔】java工程中JSON 字符串格式化输出

2023-12-20 06:52:34

一,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", "编程语言" ]
  }
} 

三,源码传送

https://gitee.com/00fly/effict-side/blob/master/springboot-cache/src/test/java/com/fly/test/restful/json/JsonPrettyFormat.java

文章来源:https://blog.csdn.net/qq_16127313/article/details/135087188
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。