如何检查“isEmpty()”在Gson JsonObject中? [英] How to check "isEmpty()" in Gson JsonObject?
本文介绍了如何检查“isEmpty()”在Gson JsonObject中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Google Gson API来构建JSON。当我初始化一个JsonObject的时候:
JsonObject json = new JsonObject();
并打印出来,实际上是 {}
我试图排除空的JSON,即 {}
那些不是添加了任何属性。但我在Gson API中找不到类似 isEmpty()
的方法。
如何找出空JSON与Gson API?
解决方案
您可以使用 JsonObject#entrySet()
,然后检查它的 size()
。
JsonObject jsonObject = ...;
if(jsonObject.entrySet()。size()== 0){//或isEmpty()
//做某事
}
I used the Google Gson API to construct JSON. When I initialized a JsonObject with:
JsonObject json = new JsonObject();
and print it out, it was in fact {}
.
I tried to exclude the "empty" JSON, i.e. the {}
ones that were not added any properties. But I could not find a method resembling isEmpty()
in the Gson API.
How can I find out the "empty" JSON with Gson API?
解决方案
You can use JsonObject#entrySet()
then check its size()
.
JsonObject jsonObject = ...;
if (jsonObject.entrySet().size() == 0) { // or isEmpty()
// do something
}
这篇关于如何检查“isEmpty()”在Gson JsonObject中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文