json数据解析时被遗忘的一个方法

来源:互联网 发布:淘宝天猫组织架构 编辑:程序博客网 时间:2024/05/18 20:08

json数据现在在项目中用到的非常多,如解析{“name”:”zhangsan“},当我们用getString(”age“)时,此时会抛出异常

123
Returns the value mapped by name if it exists, coercing it if necessary.ThrowsJSONException  if no such mapping exists.  

为避免出现此异常,我们可以用另外一个方法optString(”age“),这样即便没有这个字段也不会抛出异常,而是返回空字符。详情看这个方法的英文注释

1
Returns the value mapped by name if it exists, coercing it if necessary. Returns the empty string if no such mapping exists. 

声明:eoe文章著作权属于作者,受法律保护,转载时请务必以超链接形式附带如下信息

原文作者: 皇马船长

原文地址: http://my.eoe.cn/cainiao1/archive/15406.html