且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查“isEmpty()”在Gson JsonObject中?

更新时间:2023-11-26 09:58:10

您可以使用 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
}