fastjosn toJSONString parseObject 保留null值,保持字段顺序

fastjosn

SerializerFeature.WriteMapNullValue(JSON.toJSONString)

转jsonString时对于value的值使用“SerializerFeature.WriteMapNullValue”可保留null值

public static void main(String[] args) {
        List<Map<String, String>> list = new ArrayList<>();
        Map map1 = new HashMap(2);
        map1.put("id", "66217");
        map1.put("name", null);
        Map map2 = new HashMap(2);
        map2.put("id", "98504");
        map2.put("name", null);
        list.add(map1);
        list.add(map2);
        JSONArray jsonArray = JSON.parseArray(JSON.toJSONString(list, new JsonFilter()));
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(map1, new JsonFilter()));
        System.out.println("使用JsonFilter过滤器过滤,list转jsonArray " + jsonArray);
        System.out.println("使用JsonFilter过滤器过滤,list转jsonObject " + jsonObject);
        //转jsonString时对于value的值使用”SerializerFeature.WriteMapNullValue“保留null值
        String jsonString = JSON.toJSONString(map1, SerializerFeature.WriteMapNullValue);
        System.out.println("map转jsonString " + jsonString);
        String jsonString1 = JSON.toJSONString(list, SerializerFeature.WriteMapNullValue);
        System.out.println("list转jsonString " + jsonString1);
        String result = "{\"errorCode\":null,\"errorMsg\":null,\"result\":[{\"id\":\"123\",\"name\":null,\"age\":\"1\"},{\"id\":\"1234\",\"name\":null,\"age\":\"1\"}],\"success\":true}";
        String jsonString2 = JSON.toJSONString(result, SerializerFeature.WriteMapNullValue);
        System.out.println("String转jsonString " + jsonString2);
    }

JsonFilter过滤器类
虽然过滤器可以将键值对中value为null的值转换为空字符串打印出来,但使用场景受限

public class JsonFilter implements ValueFilter {

    @Override
    public Object process(Object o, String s, Object v) {
        if (v == null) {
            return "";
        }
        return v;
    }
}

运行结果如下:
1、过滤器将value为null的值转化为了空字符串
2、“SerializerFeature.WriteMapNullValue”将value为null的值保留了下来
在这里插入图片描述

new TypeReference<Map<String,Object>>(){}(JSONObject.parseObject)

1、转JSONObject时对于value的值使用“new TypeReference<Map<String,Object>>(){}”可保留null值,但对于json数组中value为null的值没有效果
2、Feature.OrderedField 保持字段顺序不乱序

public static void main(String[] args) {

        String result ="{\"errorCode\":null,\"errorMsg\":null,\"result\":[{\"id\":\"123\",\"name\":null,\"age\":\"1\",\"height\":\"123\",\"weight\":\"123\"},{\"id\":\"1234\",\"name\":null,\"age\":\"1\",\"height\":\"123\",\"weight\":\"123\"}],\"success\":true}";
        JSONObject jsonObject1 = JSON.parseObject(result);
        System.out.println("直接打印result" + jsonObject1);
        System.out.println("/");
        //new TypeReference<Map<String,Object>>(){} 可以将键值对中value为null的值保留下来,但对于json字符串数组内的null值保存不下来
        //Feature.OrderedField 将转换为json的字段顺序保持一致
        Map<String,Object> map = JSONObject.parseObject(result,new TypeReference<Map<String,Object>>(){}, Feature.OrderedField);
        System.out.println(map);
        System.out.println("/");
        //SerializerFeature.WriteMapNullValue == WriteMapNullValue
        //对于json字符串数组内的null值使用JSONObject.toJSONString(map, SerializerFeature.WriteMapNullValue)保留下来,但是键上会多出双引号
        String jsonString = JSONObject.toJSONString(map, SerializerFeature.WriteMapNullValue);
        System.out.println(jsonString);
}

在这里插入图片描述
ps:网上有一种说法,对于转json时的null值实际上是存在的,只是没打印出来。
另外对于数据内的null值,各位大佬有好的解决办法麻烦评论区踢我一下。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值