安卓解析中JSON中getString() 和optString()的区别

来源:互联网 发布:软件培训会务准备 编辑:程序博客网 时间:2024/04/19 20:03
ThedifferenceisthatoptSt;这个是在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.

这个是在stackoverflow上面看到的,mark一下:

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

---------------

可以解决服务器字段缺少或者没有该字段而导致的异常以至于程序崩溃。

0 0
原创粉丝点击