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>() {});

相关文章:

  • 2022-02-25
  • 2021-09-18
  • 2022-12-23
  • 2021-12-14
  • 2022-12-23
  • 2022-12-23
  • 2021-05-15
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-15
  • 2021-09-06
  • 2022-12-23
相关资源
相似解决方案