//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对象时,抛出异常;
json [{"CarId":1},{"CarSpeed",132}]
JSONArray
jsonArray = new JSONArray((String)msg.obj);
Object
reqOb = jsonArray.optJSONObject(0);
Object resOb = jsonArray.optJSONObject(1);
本文解析了optJSONObject与getJSONObject两个方法的源码实现。optJSONObject方法在获取的值不是JSONObject时返回null,不会抛出异常;而getJSONObject则会在获取到的值不是JSONObject时抛出JSONException异常。
6162

被折叠的 条评论
为什么被折叠?



