问题描述
我收到的JSON对象如下所示:
The JSON object I'm receiving looks like this:
[{"foo1":"bar1", "foo2":"bar2", "problemkey": "problemvalue"}]
我想要的是什么test for是否存在 problemvalue
。如果 problemvalue
返回一个JSON对象,我很高兴。如果没有,它将以 {}
的形式返回。我该如何测试这种情况?我尝试了几件事无济于事。
What I'm trying to test for is the existence of problemvalue
. If problemvalue
returns a JSON Object, I'm happy. If it doesn't, it will return as {}
. How do I test for this condition? I've tried several things to no avail.
这是我到目前为止所尝试的:
This is what I've tried thus far:
// if (obj.get("dps") == null) { //didn't work
// if (obj.get("dps").equals("{}")) { //didn't work
if (obj.isNull("dps")) { //didn't work
System.out.println("No dps key");
}
我预计这些行之一会打印No dps key,因为 {dps:{}}
,但无论出于何种原因,事实并非如此。我正在使用 org.json
。 jar文件是 org.json-20120521.jar
。
I expected one of these lines to print "No dps key" because {"dps":{}}
, but for whatever reason, it's not. I'm using org.json
. The jar file is org.json-20120521.jar
.
推荐答案
obj.length() == 0
是我的意思做。
这篇关于如何在Java中测试JSON对象是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!