今天在android4.4.2(android-19)平台上做Json数据解析的时候碰到了这样一个问题:
返回的数据为:{"message":null}
当使用:JSONObject jsonObj = new JSONObject(jsonMessage);
String message=jsonObj.getString("message");
本以为message 是空值(null)
结果总不能如愿,最后发现:getString(),如果value是空值,那么这个方法会返回一个“null"字面量,而不是null对象。
源码:
/**
* Returns the value mapped by {@code name} if it exists, coercing it if
* necessary.
*
* @throws JSONException if no such mapping exists.
*/
public String getString(String name) throws JSONException {
Object object = get(name);
String result = JSON.toString(object);
if (result == null) {
throw JSON.typeMismatch(name, object, "String");
}
return result;
}
本文记录了在Android平台上使用JSONObject解析特定JSON数据时遇到的问题。当JSON字段值为null时,getString()方法不会返回null对象,而是返回字符串null。文章通过实例展示了此行为,并提供了源码解释。
3万+

被折叠的 条评论
为什么被折叠?



