The difference between getString() and optString() in Json
Asked Answered
S

5

82

What is the difference between getString() and optString() in JSON?

Socialism answered 9/12, 2012 at 19:6 Comment(1)
optString can be useful in situations where you aren't confident that the JSON request format will remain the same... i.e., calling getString on a JSON request might work at first, but if it changes in the future such that the key no longer exists, an exception will be thrown and your app may crash.Staid
B
140

As Diego mentions, it's a good idea to check the documentation (this link is now out of date - good thing we have the Wayback Machine!) before posting a question here, but now that you have:

The difference is that optString returns the empty string ("") if the key you specify doesn't exist. getString on the other hand throws a JSONException. Use getString if it's an error for the data to be missing, or optString if you're not sure if it will be there.

Edit: Full description from the documentation:

Get an optional string associated with a key. It returns an empty string if there is no such key. If the value is not a string and is not null, then it is converted to a string.

Brainchild answered 9/12, 2012 at 19:14 Comment(2)
FYI: providing a second string as a parameter will be used as a default value if the key specified doesn't exist.Hatbox
Upvoting cuz I got to know about the Wayback Machine. It's super amazing.Carter
B
13

If you want to avoid NullPointerException you better make use of optString()

If you are fetching the data from JSON at any time, you might have null data for a particular Key value, at that time instead of implementing Null conditions, better make use of this optimized method optString("<keyname>")

Buchheim answered 9/8, 2016 at 8:31 Comment(0)
E
1

public java.lang.String optString(int index) Get the optional string value associated with an index. It returns an empty string if there is no value at that index. If the value is not a string and is not null, then it is coverted to a string. Parameters: index - The index must be between 0 and length() - 1. Returns: A String value.

Eliseelisee answered 3/9, 2015 at 6:31 Comment(0)
E
1

1) getString (String name):- This method Returns the String value mapped by name if it exists, coercing it if necessary, or throws JSONException if no such mapping exists.

2)optString (String name):- This method Returns the String value mapped by name if it exists, coercing it if necessary, or the empty string ("") if no such mapping exists.

Eliseelisee answered 9/9, 2019 at 5:33 Comment(0)
B
1

optString() is used to overcome NullPointerException, which we get while using getString() when the required key doesn't exists in json it basically replaces with the default value.

example let the input Json be

{
"name":"abhi",
"country":"india"
}

now in java when you execute

String city = json.getString("city");

it will throw a NullPointerException.

by using optString(String key, String default) we can overcome the above problem.

String city= json.optString("city","default");

System.out.println(city);

Output: default

Blucher answered 9/9, 2019 at 6:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.