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.
这个是在stackoverflow上面看到的,mark一下,简单来说就是optString会在得不到你想要的值时候返回空字符串”“,而getString会抛出异常。
本文介绍了JSON解析中optString与getString两个方法的区别:当指定键不存在时,optString返回空字符串,而getString则抛出异常。文章建议在数据缺失视为错误时使用getString,在不确定数据是否存在时使用optString。
4065

被折叠的 条评论
为什么被折叠?



