FastJson把json串转为JSONObject/string时,保留值为null的字段

alibaba的JSON.toString默认会把值为null的字段去掉。例如:

 Map map = new HashMap<>();
        map.put("a", "1");
        map.put("b", "2");
        map.put("c", null);
        String jsonString = JSON.toJSONString(map);
        System.out.println(jsonString);

结果:{"a":"1","b":"2"}

    Map map = new HashMap<>();
        map.put("a", "1");
        map.put("b", "2");
        map.put("c", "");
        String jsonString = JSON.toJSONString(map);
        System.out.println(jsonString);

  结果:{"a":"1","b":"2","c":""}

================================如果想保留值为null的字段,请使用

JSONObject.toJSONString(JSONObject对象, SerializerFeature.WriteMapNullValue)

你可能感兴趣的:(技术分享,工作心得)