加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

JSONObject中optString和getString等的区别

发布时间:2020-12-16 18:55:38 所属栏目:百科 来源:网络整理
导读:同事在看到我写的解析数据代码后,告诉我optString比getString好用,optString不会抛异常,而getString会抛异常,自己是将信将疑,就说,回去后我查查资料。 在在stackoverflow上面看到一个解释如下: The difference is that optString returns the empty s

同事在看到我写的解析数据代码后,告诉我optString比getString好用,optString不会抛异常,而getString会抛异常,自己是将信将疑,就说,回去后我查查资料。

在在stackoverflow上面看到一个解释如下:

The difference is thatoptStringreturns 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.

简单来说就是optString会在得不到你想要的值时候返回空字符串”“,而getString会抛出异常。至此,疑惑已经解除。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读