optString () is used to overcome the NullPointerException that we get when using getString (), when the required key does not exist in json, it is basically replaced with the default value.
For example, let Json input be
{ "name":"abhi", "country":"india" }
now in java when you execute
String city = json.getString("city");
this will throw a NullPointerException .
with optString(String key, String default) we can overcome the above problem.
String city= json.optString("city","default"); System.out.println(city);
Conclusion: default
abhigeek Sep 09 '19 at 6:30 2019-09-09 06:30
source share