zoukankan      html  css  js  c++  java
  • fastjson中的坑 map中空字符问题

    JSON.toJSONString中序列化map中的空字符串会出现空对象问题

    Map<String,String> map = new HashMap<>();
            map.put("aaa",null);
            map.put("bbb",null);
            map.put("ccc",null);
            map.put("ddd",null);
            System.out.println(map.toString());//打印结果:{aaa=null, ccc=null, bbb=null, ddd=null}
            String s = JSONObject.toJSONString(map);
            System.out.println(s);//打印结果:{} 造成空对象
    
    

    解决方法:

    通过String s = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue)

    Map<String,String> map = new HashMap<>();
            map.put("aaa",null);
            map.put("bbb",null);
            map.put("ccc",null);
            map.put("ddd",null);
            System.out.println(map.toString());
            String s = JSONObject.toJSONString(map, SerializerFeature.WriteMapNullValue);//打印结果:{aaa=null, ccc=null, bbb=null, ddd=null}

        System.out.println(s);//打印结果:{"aaa":null,"ccc":null,"bbb":null,"ddd":null}
    
    

      


    Json转某个对象时,使用这个方法

    DeviceLaserStatus deviceLaserStatus = JSON.parseObject(objectToJson, new TypeReference<DeviceLaserStatus>() {});
  • 相关阅读:
    fstest
    iozone
    fio
    vdbench
    饼状图点击凸出,适合颜色选择
    个人常用的win7快捷键
    form表单提交数据
    jquery允许跨越获取cookie
    设置滚动条样式与监听滚动到底部
    设置滚动条样式与监听滚动到底部
  • 原文地址:https://www.cnblogs.com/mikemhm/p/11551666.html
Copyright © 2011-2022 走看看