當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JSONObject中optString和getString等的区别
生活随笔
收集整理的這篇文章主要介紹了
JSONObject中optString和getString等的区别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
? ? 同事在看到我寫的解析數據代碼后,告訴我optString比getString好用,optString不會拋異常,而getString會拋異常,自己是將信將疑,就說,回去后我查查資料。
? ? 在在stackoverflow上面看到一個解釋如下:
? ??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.
? ??簡單來說就是optString會在得不到你想要的值時候返回空字符串”“,而getString會拋出異常。至此,疑惑已經解除。
? ??
轉載于:https://my.oschina.net/deepSprings/blog/729834
總結
以上是生活随笔為你收集整理的JSONObject中optString和getString等的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++中BOOL和bool的区别
- 下一篇: 栈的基本操作(C/C++)