使用的版本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
2条答案
按热度按时间gstyhher1#
设计就是这样,故意把null算为not contains,你觉得有问题么?
ahy6op9u2#
@wenshao
当指定json path不存在的情况下,如($.c),返回null
当指定json path存在的情况下,且值为null,如($.a),返回null
在这两种情况下没有办法区分,或者说,有类似于containsKey 的api可供判断吗?