jsonObject.getString() 和jsonObject.optString()区别

来源:互联网 发布:科比2016数据 编辑:程序博客网 时间:2024/03/28 23:28

//optJSONObject源码解析:    
     /**
     * Returns the value mapped by {@code name} if it exists and is a {@code
     * JSONObject}. Returns null otherwise.
     */
    publicJSONObject optJSONObject(String name) {
        Object object = opt(name);
        returnobject instanceofJSONObject ? (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}.
     */
    publicJSONObject getJSONObject(String name) throwsJSONException {
        Object object = get(name);
        if(object instanceofJSONObject) {
            return(JSONObject) object;
        }else{
            throwJSON.typeMismatch(name, object, "JSONObject");
        }
    }
    //当返回值不是JSONObject对象时,抛出异常;
1 0