«

JSON解析的两个函数

时间:2024-3-2 16:56     作者:韩俊     分类: Javascript


/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对象时,抛出异常;

标签: javascript

热门推荐