getString()
和optString()
在JSON中有什么区别?
最佳答案
正如 Diego 所说,检查 documentation 是个好主意(这个链接现在已经过时了 - 幸好我们有 Wayback Machine! )在这里发布问题之前,但现在你有:
不同之处在于,如果您指定的键不存在,optString
会返回空字符串 (""
)。另一方面,getString
会抛出 JSONException
。如果数据丢失是一个错误,请使用 getString
,如果您不确定它是否会出现,请使用 optString
。
编辑:文档中的完整描述:
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.
关于java - Json中getString()和optString()的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13790726/