getString() VS optString() in Json

SHISHIR
1 min readOct 21, 2018

--

  • optString("<KeyName>") returns the empty string ("") if the key you specify doesn't exist. On the other hand getString("<KeyName>") throws a JSONException.

While fetching the data from JSON , we 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>")

--

--

SHISHIR
SHISHIR

Written by SHISHIR

{ 'designation' : 'Lead Software Engineer' , 'hobby' : [ 'Music', 'Photography', 'Travelling' ] ,’email’: ‘shishirthedev@gmail.com’ }

No responses yet