How to check "isEmpty()" in Gson JsonObject?
You can use JsonObject#entrySet()
to get the JSON object's set of name/value pairs (its members). That returns a Set
which has the traditional isEmpty()
method you're looking for.
For example,
JsonObject jsonObject = ...;
Set<Map.Entry<String,JsonElement>> members = jsonObject.entrySet();
if (members.isEmpty()) {
// do something
}
Its more correct to use the Set.isEmpty() for this purpose
if (jsonObject.entrySet().isEmpty()) {
}