类型为org.json.JSONObject的数据无法转换为JSONArray [英] at data of type org.json.JSONObject cannot be converted to JSONArray
本文介绍了类型为org.json.JSONObject的数据无法转换为JSONArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的答复
{
"status": "success",
"statuscode": 200,
"message": "Record found successfully.",
"data": {
"tangible_benefits": "ds1351gsghsdh353535535",
"intangible_benefits": "shwryw24y43rwehdg135313513",
"total_annual_savings": "45135432",
"root_cause_identification": [
{
"id": "6",
"projectid": "1498",
"step": "6",
"root_cause_identified": "efficiency",
"solution_implemented": "efficiency",
"implementaion_date": "14-01-2020",
"createdby": "201465",
"updatedby": "201465",
"created_date": "2020-01-14 18:04:41",
"updated_date": "2020-01-14 18:04:41"
}
]
}
}
java代码
try {
JSONObject res = new JSONObject(response);
if (res.getString("status").equalsIgnoreCase("success")) {
JSONArray TQMData = res.getJSONArray("data");
for (int i = 0; i < TQMData.length(); i++) {
JSONObject obj = TQMData.getJSONObject(i);
stepsList.add(new TQMSavedDataModel(obj.getString("tangible_benefits"),
obj.getString("intangible_benefits"),
obj.getString("total_annual_savings"),
(List<RootCauseIdentificationModel>) obj.getJSONObject("root_cause_identification")
));
}
}
} catch (JSONException e) {
e.printStackTrace();
dialog.dismiss();
Toast.makeText(getActivity(), "Something went wrong, please try again.", Toast.LENGTH_SHORT).show();
getActivity().finish();
}
推荐答案
这是正确的答案.
try {
JSONObject res = new JSONObject(response);
if (res.getString("status").equalsIgnoreCase("success")) {
JSONObject obj = res.getJSONObject("data");
JSONArray rootCauseIdentificationArray = obj.getJSONArray("root_cause_identification");
for(int i = 0; i<rootCauseIdentificationArray.length(); i++){
JSONObject objData = rootCauseIdentificationArray.getJSONObject(i);
//iterate the object here and add to the list
RootCauseIdentificationModel model = new RootCauseIdentificationModel( objData.getString("id"), objData.getString("root_cause_identified"), objData.getString("solution_implemented"), objData.getString("implementaion_date"));
dataList.add(model);
}
stepsList.add(new TQMSavedDataModel(obj.getString("tangible_benefits"),
obj.getString("intangible_benefits"),
obj.getString("total_annual_savings"),
dataList));
tangibleBenefits.setText(obj.getString("tangible_benefits"));
intangibleBenefits.setText( obj.getString("intangible_benefits"));
annualAmount.setText(obj.getString("total_annual_savings"));
}
} catch (JSONException e) {
e.printStackTrace();
dialog.dismiss();
Toast.makeText(getActivity(), "Something went wrong, please try again.", Toast.LENGTH_SHORT).show();
getActivity().finish();
}
这篇关于类型为org.json.JSONObject的数据无法转换为JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文