您好,登錄后才能下訂單哦!
本篇文章為大家展示了Java 如何實現格式化輸出JSON字符串,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
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 如何實現格式化輸出JSON字符串,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。