請(qǐng)求服務(wù)器后,服務(wù)器會(huì)返回一串json.寫(xiě)的久了摸索了一套解析方式.留下模版
{
“success”:true,
“code”:”1000”,
”message”:”用戶已登錄”,//用戶未登錄
”token”:”tokenfslfjsdfsf”,//未登錄為null或””
”resultData”:{
“bannerList”:[{}蚕断,{}],
”deveiceList”:[{}斩萌,{}]//未登錄為null
}
}
{
"code":"50002",
"message":"版本可選更新",
"resultData":{
"foreUpdate":false,
"updateUrl":"http://127.0.0.1/android/test"
},
"success":true,
"token":"tokenfslfjsdfsf"
}
如上.服務(wù)固定返回五個(gè)數(shù)據(jù),其中只有resultData不同
想法就是建立一個(gè)BaseBean class 然后其他都繼承
public class BaseBean {
public String token;
public boolean success;
public String code;
public String message;
JSONObject resultData ;
}
剩下的繼承
public class UpdateBean extends BaseBean {
public boolean foreUpdate;
public String updateUrl;
//只做解析工作然后返回當(dāng)前對(duì)象
public static UpdateBean parseJson(String json) {
UpdateBean updateBean = new UpdateBean();
try {
JSONObject jsonObject = new JSONObject(json);
updateBean.success = jsonObject.optBoolean("success");
if (updateBean.success) {
updateBean.token = jsonObject.optString("token");
updateBean.code = jsonObject.optString("code");
updateBean.message = jsonObject.optString("message");
updateBean.resultData = jsonObject.optJSONObject("resultData");
updateBean.updateUrl = updateBean.resultData.optString("updateUrl");
updateBean.foreUpdate = updateBean.resultData.optBoolean("foreUpdate");
}
} catch (JSONException e) {
e.printStackTrace();
}
return updateBean;
}
}
留下足跡