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>() {});
  • 相关阅读:
    YXY-压测
    禅道使用
    抓https包
    数据库基本查询语句
    限制网速 制造测试条件
    测试中认识 sqlite
    Adb 命令
    jmeter 查看提取的参数
    jmeter传入字符时文本显示乱码
    nginx 访问springboot项目
  • 原文地址:https://www.cnblogs.com/mikemhm/p/11551666.html
Copyright © 2011-2022 走看看