javascript
JSONObject中optString和getString等的区别
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
? ? 同事在看到我寫的解析數(shù)據(jù)代碼后,告訴我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會拋出異常。至此,疑惑已經(jīng)解除。
? ??
轉(zhuǎn)載于:https://my.oschina.net/deepSprings/blog/729834
總結(jié)
以上是生活随笔為你收集整理的JSONObject中optString和getString等的区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++中BOOL和bool的区别
- 下一篇: JStorm2.1.1集群的安装和使用