亲测:字符串提前保留为空串,数字为0 WriteMapNullValue WriteNullStringAsEmpty WriteNullNumberAsZero 这3个同时使用
从输出结果可以看出,null对应的key已经被过滤掉;这明显不是我们想要的结果,这时我们就需要用到fastjson的SerializerFeature序列化属性
也就是这个方法: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
|
1 2 3 4 5 6 7 8 9 |
private static final SerializerFeature[] serializerFeatures; //... static { serializerFeatures = new SerializerFeature[] { SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty }; } JSON.toJSONString(msgMap, serializerFeatures) |
转自:https://blog.csdn.net/qq_34412985/article/details/81985459
from: https://www.codeleading.com/article/2025308079/