fastjson 可以设置 null值 转为
SerializerFeature.WriteNullStringAsEmpty
但是这个属性只针对实体,对 map这种结构不起效
此时需要如下所示,会将全部的null值转换为空字符串
但是特别注意,null值的时候丢失了原数据类型,所以不管数字,日期,或者其他的都变成了""
所以如果有可能还是不要偷懒,尽量用实体
/*
map值过滤为"",以下的配置只针对实体起效
SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty*/
public void valuefilter(){
Map<String,Object> map=new HashMap<>();
map.put("name",null);
map.put("sex","男");
map.put("age",12);
map.put("ECUCATION",12);
map.put("people","汉");
map.put("ti",null);
ValueFilter filter = new ValueFilter() {
@Override
public Object process(Object obj, String s, Object v) {
if(v==null)
return "";
return v;
}
};
System.out.println(JSONObject.toJSONString(map,filter
,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty));
}
/*
map的key值统一为小写,以下的配置只针对实体起效
SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty*/
public void namefilter(){
Map<String,Object> map=new HashMap<>();
map.put("name",null);
map.put("sex","男");
map.put("age",12);
map.put("ECUCATION",12);
map.put("people","汉");
map.put("ti",null);
NameFilter filter = new NameFilter() {
@Override
public String process(Object obj, String s, Object v) {
return s.toLowerCase();
}
};
System.out.println(JSONObject.toJSONString(map,filter
,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty));
}
public static void main(String[] args) {
FastJsonTest fastJsonTest=new FastJsonTest();
//fastJsonTest.valuefilter();
fastJsonTest.namefilter();
}