fastjson 当值为null时JSONPath.contains判断有误

llew8vvj  于 2022-11-05  发布在  其他
关注(0)|答案(2)|浏览(132)

使用的版本1.2.12

Map<String, String> map = new HashMap<>();
        map.put("a", null);
        map.put("b", "1");

        String x = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);
        System.out.println(x);

        JSONObject jsonObject = JSON.parseObject(x);
        System.out.println(JSONPath.contains(jsonObject, "$.a") + "\t" + jsonObject.containsKey("a"));
        System.out.println(JSONPath.contains(jsonObject, "$.b") + "\t" + jsonObject.containsKey("b"));

Output:
{"b":"1","a":null}
false true
true true

gstyhher

gstyhher1#

设计就是这样,故意把null算为not contains,你觉得有问题么?

ahy6op9u

ahy6op9u2#

@wenshao

当指定json path不存在的情况下,如($.c),返回null
当指定json path存在的情况下,且值为null,如($.a),返回null

在这两种情况下没有办法区分,或者说,有类似于containsKey 的api可供判断吗?

相关问题