Json解析值get与opt的区别

来源:互联网 发布:里约奥运会直播软件 编辑:程序博客网 时间:2024/05/22 12:26
//optJSONObject源码解析:     
     /**
     * Returns the value mapped by {@code name} if it exists and is a {@code
     * JSONObject}. Returns null otherwise.
     */
    public JSONObject optJSONObject(String name) {
        Object object = opt(name);
        return object instanceof JSONObject ? (JSONObject) object : null;
    }
    //当返回值不是JSONObject对象时,返回值为null,不抛出异常;
 
 
//getJSONObject源码解析:
     /**
     * Returns the value mapped by {@code name} if it exists and is a {@code
     * JSONObject}.
     * @throws JSONException if the mapping doesn't exist or is not a {@code
     * JSONObject}.
     */
    public JSONObject getJSONObject(String name) throws JSONException {
        Object object = get(name);
        if (object instanceof JSONObject) {
            return (JSONObject) object;
        } else {
            throw JSON.typeMismatch(name, object, "JSONObject");
        }
    }
    //当返回值不是JSONObject对象时,抛出异常;
0 0
原创粉丝点击