Android getJSONObject與optJSONObject的區(qū)別結合源碼分析
更新時間:2017年02月25日 14:21:26 投稿:lqh
這篇文章主要介紹了Android getJSONObject與optJSONObject的區(qū)別,結合源碼分析的相關資料,需要的朋友可以參考下
Android getJSONObject與optJSONObject的區(qū)別結合源碼分析
json解析常見問題:
getJSONObject與optJSONObject的區(qū)別,下面結合源碼和案例來分析當我們使用這兩周方法來解析數(shù)據(jù)時,哪種比較好.
源碼分析:
//使用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數(shù)據(jù)
*
* @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)會莫名解析不了下面數(shù)據(jù):如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自定義View實現(xiàn)數(shù)獨游戲
這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)數(shù)獨游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Flutter Navigator路由傳參的實現(xiàn)
本文主要介紹了Flutter Navigator路由傳參的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
Android 多種dialog的實現(xiàn)方法(推薦)
下面小編就為大家分享一篇Android 多種dialog的實現(xiàn)方法(推薦),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
打飛機游戲終極BOSS Android實戰(zhàn)打飛機游戲完結篇
打飛機游戲終極BOSS,Android實戰(zhàn)打飛機游戲完結篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07

