Java 格式化輸出JSON字符串的2種實(shí)現(xiàn)操作
1 使用阿里的FastJson
1.1 項(xiàng)目的pom.xml依賴
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.58</version></dependency>
1.2 Java示例代碼
(1) 導(dǎo)入的包:
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': '機(jī)械工業(yè)出版社','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': '機(jī)械工業(yè)出版社','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) 格式化輸出后的結(jié)果:
說明: FastJson通過Tab鍵進(jìn)行換行后的格式化.
{ '_index':'book_shop', '_type':'it_book', '_source':{ 'date':'2007-06-01', 'author':'[美] Bruce Eckel', 'price':109.0, 'name':'Java編程思想(第4版)', 'publisher':'機(jī)械工業(yè)出版社', 'category':'編程語言', 'tags':[ 'Java', '編程語言' ] }, '_id':'1', '_score':1.0}
2 使用谷歌的Gson
2.1 項(xiàng)目的pom.xml依賴
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.4</version></dependency>
2.2 Java示例代碼
(1) 導(dǎo)入的包:
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': '機(jī)械工業(yè)出版社','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) 格式化輸出后的結(jié)果:
說明: Gson使用2個(gè)空格作為換行后的格式轉(zhuǎn)換.
{ '_index': 'book_shop', '_type': 'it_book', '_id': '1', '_score': 1.0, '_source': { 'name': 'Java編程思想(第4版)', 'author': '[美] Bruce Eckel', 'category': '編程語言', 'price': 109.0, 'publisher': '機(jī)械工業(yè)出版社', 'date': '2007-06-01', 'tags': [ 'Java', '編程語言' ] }}
以上這篇Java 格式化輸出JSON字符串的2種實(shí)現(xiàn)操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. phpstudy apache開啟ssi使用詳解2. CentOS郵件服務(wù)器搭建系列—— POP / IMAP 服務(wù)器的構(gòu)建( Dovecot )3. .NET SkiaSharp 生成二維碼驗(yàn)證碼及指定區(qū)域截取方法實(shí)現(xiàn)4. IntelliJ IDEA創(chuàng)建web項(xiàng)目的方法5. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼6. docker容器調(diào)用yum報(bào)錯(cuò)的解決辦法7. ASP中實(shí)現(xiàn)字符部位類似.NET里String對(duì)象的PadLeft和PadRight函數(shù)8. django創(chuàng)建css文件夾的具體方法9. MyBatis JdbcType 與Oracle、MySql數(shù)據(jù)類型對(duì)應(yīng)關(guān)系說明10. javascript xml xsl取值及數(shù)據(jù)修改第1/2頁
