In org.json, how to do getString(), getBoolean() on a value of a key by first checking if it exists, without writing 'has' code to avoid exception

37 views Asked by At

If a key does not exist, on doing get, will throw exception. Would like to avoid writing if jsonObject.has("key"), if true, then jsonObject.get("key") for each get.

1

There are 1 answers

0
JP Belanger On

You can use the opt*() methods. Those that can return null, will return null, and others retrun a reasonable default value (or take a defaultValue parameter).