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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM