加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

JSONObject #getString(String string) 空值(null)问题

发布时间:2020-12-16 19:16:05 所属栏目:百科 来源:网络整理
导读:今天在android4.4.2(android-19)平台上做Json数据解析的时候碰到了这样一个问题: 返回的数据为:{"message":null} 当使用:JSONObject jsonObj = new JSONObject(jsonMessage); String message=jsonObj.getString("message"); 本以为message 是空值(null) 结

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读