今天在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;
- }