解决 null 在转json 时丢失的问题

1.丢失效果

 public static void main(String[] args) {
        User user = new User();
        Map<String, Object> reMap = new HashMap<String, Object>();
        reMap.put("aaa",null);
        reMap.put("bbb",user);
        String json = JSONObject.toJSONString(reMap);
        System.out.println(json);
    }

在这里插入图片描述
2.不丢失效果(SerializerFeature.WriteMapNullValue)(SerializerFeature.WRITE_MAP_NULL_FEATURES)

 public static void main(String[] args) {
        User user = new User();
        Map<String, Object> reMap = new HashMap<String, Object>();
        reMap.put("aaa",null);
        reMap.put("bbb",user);
        String json = JSONObject.toJSONString(reMap,SerializerFeature.WriteMapNullValue);
        String json1 = JSONObject.toJSONString(reMap,SerializerFeature.WRITE_MAP_NULL_FEATURES);
        System.out.println("json:"+json);
        System.out.println("json1:"+json1);
    }

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/WXN069/article/details/91981542