Skip to content
Advertisement

is there a way to check for existence of a key in JSONObject of org.json java package?

Essentially its as the title says, I want to check if key “error” exists in the JSONObject but guarding it with try catch as a part of normal control flow seems to be a very bad and dirty thing to do, so surely it cant be the intended solution. But I cant think of any other way to do it.

Advertisement

Answer

There is a has method to check if the key exists

You can also use one of the opt methods so that you can get back a default value in case the key doesn’t exist

Advertisement