java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java JSON格式化

java实现JSON字符串格式化输出

作者:爱码少年

这篇文章主要为大家详细介绍了如何使用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", "编程语言" ]
  }

三、源码传送

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

到此这篇关于java实现JSON字符串格式化输出的文章就介绍到这了,更多相关java JSON字符串格式化输出内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文