码迷,mamicode.com
首页 > 编程语言 > 详细

Java - 格式化输出JSON字符串的两种方式

时间:2019-07-16 18:22:03      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:obj   name   说明   parser   imp   apn   pen   category   tty   

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

参考资料

JAVA-Gson-格式化输出json字符串

Java - 格式化输出JSON字符串的两种方式

标签:obj   name   说明   parser   imp   apn   pen   category   tty   

原文地址:https://www.cnblogs.com/shoufeng/p/11196500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!