Thuy Trinh
Thuy Trinh

Reputation: 2914

Gson - proper ways to check for node/element existance

When I have a JsonObject, in order to retrieve a specified element member from it, there would be a couple of methods, for instance, get() and other getAsXXX():

JsonPrimitive childNode1 = parent.getAsJsonPrimitive("key1);
JsonArray childNode2 = parent.getAsJsonArray("key2);
JsonElement childNode3 = parent.get("key3);

My question is that, is isJsonNull() the way to check if an element exists? I got a bit confused by Json's documentation.

Upvotes: 3

Views: 9962

Answers (1)

Sean
Sean

Reputation: 179

You can use JsonObject.has() in order to determine if an element exists.

See code definition.

Upvotes: 7

Related Questions