最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用JSONObject.toJSONString 過濾掉值為空的key

 更新時間:2022年03月03日 08:55:53   作者:起名-困難戶  
這篇文章主要介紹了使用JSONObject.toJSONString 過濾掉值為空的key,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

JSONObject.toJSONString 過濾值為空的key

情況

public static String getJsonResult(int status, String msg, Object data){undefined
? ? ? ? Map<String, Object> resultMap=new HashMap<String, Object>();? ? ? ??
? ? ? ? resultMap.put("status", status);
? ? ? ? resultMap.put("msg", msg);
? ? ? ? resultMap.put("data", data);
? ? ? ? return JSONObject.toJSONString(resultMap);
? ? }
public static void main(String[] args) {undefined
? ? ? ? System.out.println(getJsonResult(1, "success", null));
? ? }

結(jié)果

{"msg":"success","status":1}

從輸出結(jié)果可以看出,null對應(yīng)的key已經(jīng)被過濾掉;這明顯不是我們想要的結(jié)果,這時我們就需要用到fastjson的SerializerFeature序列化屬性

也就是這個方法

JSONObject.toJSONString(Object object, SerializerFeature... features) ?
? ? public static String getJsonResult(int status, String msg, Object data){undefined
? ? ? ? Map<String, Object> resultMap=new HashMap<String, Object>();
? ? ? ? resultMap.put("status", status);
? ? ? ? resultMap.put("msg", msg);
? ? ? ? resultMap.put("data", data);
? ? ? ? return JSONObject.toJSONString(resultMap,SerializerFeature.WriteMapNullValue);
? ? }
public static void main(String[] args) {undefined
? ? ? ? System.out.println(getJsonResult(1, "success", null));
? ? }

結(jié)果

{"msg":"success","data":null,"status":1}

 JSONObject.toJSONString自動過濾空值

使用fastjson將javabean轉(zhuǎn)string時,默認(rèn)會將值為null的屬性過濾掉,

可通過設(shè)置SerializerFeature.WriteMapNullValue避免這種情況

String value = JSONObject.toJSONString(objectData, SerializerFeature.WriteMapNullValue);

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

连云港市| 宜城市| 塘沽区| 长海县| 鹤壁市| 富源县| 福建省| 阿拉善盟| 道孚县| 洮南市| 肥西县| 鲁甸县| 东港市| 潢川县| 陈巴尔虎旗| 四子王旗| 大连市| 莱芜市| 潞西市| 易门县| 若尔盖县| 信宜市| 韶山市| 阳山县| 肇庆市| 金华市| 康保县| 普陀区| 车险| 个旧市| 海南省| 工布江达县| 乐亭县| 自治县| 佳木斯市| 大兴区| 十堰市| 交城县| 礼泉县| 大同县| 奉化市|