日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

FastJson、Jackson、Gson进行Java对象转换Json的细节处理

發布時間:2025/4/9 java 70 豆豆
生活随笔 收集整理的這篇文章主要介紹了 FastJson、Jackson、Gson进行Java对象转换Json的细节处理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

Java對象在轉json的時候,如果對象里面有屬性值為null的話,那么在json序列化的時候要不要序列出來呢?對比以下json轉換方式

一、fastJson

1、fastJson在轉換java對象為json的時候,默認是不序列化null值對應的key的

也就是說當對象里面的屬性為空的時候,在轉換成json時,不序列化那些為null值的屬性? 具體案例如下: AutoPartsSearchRequest 有以下屬性:? public static void main(String[] args) { AutoPartsSearchRequest request = new AutoPartsSearchRequest(); request.setKeywords("123"); request.setSortingField("234242"); String str = JSONObject.toJSONString(request);//fastjson默認轉換是不序列化null值對應的key的 System.out.println(str); }
輸出結果:{"keywords":"123","sortingField":"234242"} ?, 沒有序列化那些值為null的屬性

2、但是如果想把null對應的key序列化出來呢??

那就要仔細看看fastjson轉換java對象為json的時候的入參了:也就是這個方法: JSONObject.toJSONString(Object object, SerializerFeature... features)
Fastjson的SerializerFeature序列化屬性:
?

QuoteFieldNames———-輸出key時是否使用雙引號,默認為true WriteMapNullValue——–是否輸出值為null的字段,默認為false WriteNullNumberAsZero—-數值字段如果為null,輸出為0,而非null WriteNullListAsEmpty—–List字段如果為null,輸出為[],而非null WriteNullStringAsEmpty—字符類型字段如果為null,輸出為”“,而非null WriteNullBooleanAsFalse–Boolean字段如果為null,輸出為false,而非null 結合上面,SerializerFeature... features是個數組,那么我們可以傳入我們想要的參數,比如想序列化null,案例如下: public static void main(String[] args) { AutoPartsSearchRequest request = new AutoPartsSearchRequest(); request.setKeywords("123"); request.setSortingField("234242"); String str = JSONObject.toJSONString(request, SerializerFeature.WriteMapNullValue); System.out.println(str); }
輸出結果如下:

?

3、想字符類型字段如果為null,轉換輸出為”“,而非null?,需要多加一個參數:WriteNullStringAsEmpty, 案例如下:

public static void main(String[] args) { AutoPartsSearchRequest request = new AutoPartsSearchRequest(); request.setKeywords("123"); request.setSortingField("234242"); String str = JSONObject.toJSONString(request, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty); System.out.println(str); }
輸出結果如下:

二、Jackson

1、jackson默認是序列化null對應的key的,也就是說不管你對象屬性有沒有值,在轉換json的時候都會被序列化出來

public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException { AutoPartsSearchRequest request = new AutoPartsSearchRequest(); request.setKeywords("123"); request.setSortingField("234242"); ObjectMapper mapper = new ObjectMapper(); String str = mapper.writeValueAsString(request); System.out.println(str); //輸出結果(此處就不格式化了):{"sortingField":"234242","partsClassifyId":null,"partsSubClassifyId":null,"sortingDirection":null:...... }

?

2、同理,想要不序列化null也是可以的,具體如下:

?

1.實體上@JsonInclude(Include.NON_NULL) //將該標記放在屬性上,如果該屬性為NULL則不參與序列化 //如果放在類上邊,那對這個類的全部屬性起作用 //Include.Include.ALWAYS 默認 //Include.NON_DEFAULT 屬性為默認值不序列化 //Include.NON_EMPTY 屬性為 空(“”) 或者為 NULL 都不序列化 //Include.NON_NULL 屬性為NULL 不序列化 2.代碼上 ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(Include.NON_NULL); //通過該方法對mapper對象進行設置,所有序列化的對象都將按改規則進行系列化 //Include.Include.ALWAYS 默認 //Include.NON_DEFAULT 屬性為默認值不序列化 //Include.NON_EMPTY 屬性為 空(“”) 或者為 NULL 都不序列化 //Include.NON_NULL 屬性為NULL 不序列化

?


注意:只對VO起作用,Map List不起作用,另外jackson還能過濾掉你設置的屬性,具體的就各位自己去研究源碼了

或者參照:?jackson詳解?

三、Gson


1、gson和fastjson一樣,默認是不序列化null值對應的key的,具體案例如下:

public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException { AutoPartsSearchRequest request = new AutoPartsSearchRequest(); request.setKeywords("123"); request.setSortingField("234242"); Gson g = new GsonBuilder().create(); String str = g.toJson(request); System.out.println(str); //輸出結果:{"sortingField":"234242","keywords":"123"} }

2、若是想序列化null值對應的key,只需要將以上創建代碼改成以下代碼就行:

Gson g = new GsonBuilder().serializeNulls().create(); 案例就不寫了?

3、若是想轉行null為空字符串"",則需要手動處理了

具體參考:?gson轉換null為空字符串 from:?http://www.voidcn.com/blog/shijing266/article/p-6126128.html

轉載于:https://www.cnblogs.com/GarfieldEr007/p/6822316.html

總結

以上是生活随笔為你收集整理的FastJson、Jackson、Gson进行Java对象转换Json的细节处理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。