Android getJSONObject与optJSONObject的区别结合源码分析

Android getJSONObject与optJSONObject的区别结合源码分析

json解析常见问题:

getJSONObject与optJSONObject的区别,下面结合源码和案例来分析当我们使用这两周方法来解析数据时,哪种比较好.

源码分析:

//使用getJSONObject时,如果返回的对象不是JSONObject,抛出JSONException异常

/**

* 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");

}

}

//使用optJSONObject时,当返回结果不是JSONObject时,这里不会抛异常,而是返回null

/**

* 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;

}

结合项目代码分析使用场景

/**

* 伪代码如下

**/

public class GetGoodsCollectListFactory {

private String ERROR_MSG = "errorMsg";

private String ERROR_CODE = "errorCode";

private String RESULT = "result";

/**

* 商品列表,解析获取到的json数据

*

* @param param

* @return

*/

public CollectListInfo getGoodsCollectListParse(String param) {

String BOOK_MARK_BO_LIST = "favorGoodsList";

CollectListInfo collectList = new CollectListInfo();

List<CollectInfo> list = new ArrayList<CollectInfo>();

ResponseInfo responseInfo = null;

CollectInfo info = null;

try {

// 请求获取json结果

JSONObject jsonObject = new JSONObject(HttpRequestClient.executeRequest(Constant.Url.GET_FOLLOW_GOODS_URL, param, true));

responseInfo = new ResponseInfo();

if (jsonObject.getBoolean(RESULT)) {

responseInfo.setResult(true);

JSONArray jsonArray = jsonObject.getJSONArray(BOOK_MARK_BO_LIST);

collectList.setTotalPage(jsonObject.getInt(Constant.TOTAL_PAGE_NAME));

for (int i = 0; i < jsonArray.length(); i++) {

info = collectList.getCollectInfo();

SONObject object = jsonArray.getJSONObject(i);

JSONArray activityItemList = object.optJSONArray("activityItemList");

ArrayList<ActivityCollectInfo> activityCollectInfos = new ArrayList<ActivityCollectInfo>();

//因为object.optJSONArray("activityItemList");如果解析不到会直接返null

if (activityItemList != null && activityItemList.size() > 0) {

for (int j = 0; j < activityItemList.length(); j++) {

//使用optJSONObject(i)会莫名解析不了下面数据:如optString("activityId"));

// 当activityid为null时,不会抛错,但是下面的方法不走,因此换用getJSONObject(i);

//JSONObject activityItemobj = activityItemList.optJSONObject(i);

JSONObject activityItemobj = activityItemList.getJSONObject(i);

ActivityCollectInfo activityCollectInfo = new ActivityCollectInfo();

activityCollectInfo.setActivityId(activityItemobj.optString("activityId"));

activityCollectInfo.setActivityName(activityItemobj.optString("activityName"));

activityCollectInfo.setActivityType(activityItemobj.optInt("activityType"));

activityCollectInfo.setLargessFlag(activityItemobj.optInt("largessFlag"));

activityCollectInfo.setLargessMoney(activityItemobj.optInt("largessMoney"));

activityCollectInfo.setLargessNumber(activityItemobj.optInt("largessNumber"));

activityCollectInfos.add(activityCollectInfo);

}

...

}

}

温故而知新,可以为师矣.

以上是 Android getJSONObject与optJSONObject的区别结合源码分析 的全部内容, 来源链接: utcz.com/z/314127.html

回到顶部