我读过How to check if a value/property exist in JSON data。但是这个问题对我的问题并不满意。我使用net.sf.json.JSONObject从我的数据库中检索数据。
例如:我有一个JSON对象......
{"a":"data", "b":"", "c":null, "d":2}
我测试了这个JSON中的 e 值。
if (json.get("e") == null) {
System.err.println("found in first case.");
}
else if (json.get("e").isNull()) {
System.err.println("found in second case.");
}
else {
System.err.println("All were skipped !");
}
但结果并没有像我预期的那样出现(它总是去其他情况)。我也想避免空指针异常。我怎样才能实现它?
答案 0 :(得分:0)
现在我找到了解决方案!
if(json.keySet().contains("e")) {
System.out.println("BOOM");
}
else {
System.out.println("Not contain");
}