What is the difference between getString() and optString() in JSON?
What is the difference between getString() and optString() in JSON?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
optStringreturns the empty string ("") if the key you specify doesn’t exist.getStringon the other hand throws aJSONException. UsegetStringif it’s an error for the data to be missing, oroptStringif you’re not sure if it will be there.Edit: Full description from the documentation: