JSONObject中optString和getString等的区别
2016-08-09 00:00
411 查看
摘要: org.json.JSONObject类中,在同事的提醒下发现还有optString方法,就下来搜集资料查看二者的区别,在这里记录一下,防止遗忘。
同事在看到我写的解析数据代码后,告诉我optString比getString好用,optString不会抛异常,而getString会抛异常,自己是将信将疑,就说,回去后我查查资料。
在在stackoverflow上面看到一个解释如下:
The difference is that
简单来说就是optString会在得不到你想要的值时候返回空字符串”“,而getString会抛出异常。至此,疑惑已经解除。
同事在看到我写的解析数据代码后,告诉我optString比getString好用,optString不会抛异常,而getString会抛异常,自己是将信将疑,就说,回去后我查查资料。
在在stackoverflow上面看到一个解释如下:
The difference is that
optStringreturns the empty string (
"") if the key you specify doesn't exist.
getStringon the other hand throws a
JSONException. Use
getStringif it's an error for the data to be missing, or
optStringif you're not sure if it will be there.
简单来说就是optString会在得不到你想要的值时候返回空字符串”“,而getString会抛出异常。至此,疑惑已经解除。
相关文章推荐
- JSONObject中optString和getString等的区别
- JSON中optString和getString的区别
- JSON中optString和getString的区别
- 关于JSON中optString和getString的区别
- getString和optString的区别(补充toString和valueOf)
- Json中optString和getString的区别
- (转)Field getField(String name)与Field getDeclaredField(String name)的区别
- istream::get,getline函数与string getline区别 istream::read 和ostream::write
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别
- istream::get,getline函数与string getline区别 istream::read 和ostream::write
- c++ string getline()函数 istream::get,getline函数与string getline区别 istream::read 和ostream::write
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别 [转]
- Dom4j中getText()和getStringValue()的作用和区别。
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别 [转]
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别 [转]
- istream::get,getline函数与string getline区别 istream::read 和ostream::write
- javax.servlet.ServletRequest 的getAttribute(String name)与getParmeter(String name)的区别