[代码]JSON解析的两个函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//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对象时,抛出异常;
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。