如何在Java中测试JSON集合对象是否为空 [英] How to test if JSON Collection object is empty in Java

查看:85
本文介绍了如何在Java中测试JSON集合对象是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到的JSON集合对象如下:

The JSON Collection object I'm receiving looks like this:

[{"foo1":"bar1", "foo2":"bar2", "problemkey": "problemvalue"}]

我要测试的是 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键).因为 {"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集合对象是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆