fastjson null 值转换为空字符串 key值统一大小写

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();
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值