at 类型 org.json.JSONObject 的数据无法转换为 JSONArray
at data of type org.json.JSONObject cannot be converted to 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();
}
您得到的回复是 JSONObject
而不是 JSONArray
。
首先,您应该获取 data
作为 JsonObject
然后,从这个对象中,你可以检索你需要的数组:)
try {
JSONObject res = new JSONObject(response);
if (res.getString("status").equalsIgnoreCase("success")) {
JSONObject TQMData = res.getJSONObject("data");
JSONArray arrayTQMData = TQMData.getJSONArray("root_cause_identification");
for (int i = 0; i < arrayTQMData.length(); i++) {
JSONObject obj = arrayTQMData.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 TQMData = res.getJsonObject("data");
JSONArray root_cause_identification = TQMData.getJsonArray("root_cause_identification");
for(int i=0; i< root_cause_identification.length;i++){
JsonObject root_cause_identificationObject = root_cause_identification.get(i);
// Unmarshal this for the Bean and add to list
}
}
} 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 TQMData = res.getJSONObject("data");
String tangible_benefits = TQMData.getString("tangible_benefits");
String intangible_benefits = TQMData.getString("intangible_benefits");
String total_annual_savings = TQMData.getString("total_annual_savings");
JSONArray root_cause_identification = TQMData.getJSONArray("root_cause_identification");
for (int i = 0; i < root_cause_identification.length(); i++) {
JSONObject jsonObject = root_cause_identification.getJSONObject(i);
String id = jsonObject.getString("id");
String projectid = jsonObject.getString("projectid");
String step = jsonObject.getString("step");
String root_cause_identified = jsonObject.getString("root_cause_identified");
String solution_implemented = jsonObject.getString("solution_implemented");
String implementaion_date = jsonObject.getString("implementaion_date");
String createdby = jsonObject.getString("createdby");
String updatedby = jsonObject.getString("updatedby");
String created_date = jsonObject.getString("created_date");
String updated_date = jsonObject.getString("updated_date");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
尝试下面的答案,根据您的 json 样本,您正在尝试错误地迭代 'data' 和 'root_cause_identification'
try {
JSONObject res = new JSONObject(response);
if (res.getString("status").equalsIgnoreCase("success")) {
JSONObject obj = res.getJSONObject("data");
JSONArray rootCauseIdentificationArray = obj.getJSONArray("root_cause_identification");
List<RootCauseIdentificationModel> tempList = new ArrayList<>();
for(int i = 0; i<routeCauseIdentificationArray.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"));
tempList.add(model);
}
stepsList.add(new TQMSavedDataModel(obj.getString("tangible_benefits"),
obj.getString("intangible_benefits"),
obj.getString("total_annual_savings"),
tempList));
}
} 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();
}
我的回复
{
"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();
}
您得到的回复是 JSONObject
而不是 JSONArray
。
首先,您应该获取 data
作为 JsonObject
然后,从这个对象中,你可以检索你需要的数组:)
try {
JSONObject res = new JSONObject(response);
if (res.getString("status").equalsIgnoreCase("success")) {
JSONObject TQMData = res.getJSONObject("data");
JSONArray arrayTQMData = TQMData.getJSONArray("root_cause_identification");
for (int i = 0; i < arrayTQMData.length(); i++) {
JSONObject obj = arrayTQMData.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 TQMData = res.getJsonObject("data");
JSONArray root_cause_identification = TQMData.getJsonArray("root_cause_identification");
for(int i=0; i< root_cause_identification.length;i++){
JsonObject root_cause_identificationObject = root_cause_identification.get(i);
// Unmarshal this for the Bean and add to list
}
}
} 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 TQMData = res.getJSONObject("data");
String tangible_benefits = TQMData.getString("tangible_benefits");
String intangible_benefits = TQMData.getString("intangible_benefits");
String total_annual_savings = TQMData.getString("total_annual_savings");
JSONArray root_cause_identification = TQMData.getJSONArray("root_cause_identification");
for (int i = 0; i < root_cause_identification.length(); i++) {
JSONObject jsonObject = root_cause_identification.getJSONObject(i);
String id = jsonObject.getString("id");
String projectid = jsonObject.getString("projectid");
String step = jsonObject.getString("step");
String root_cause_identified = jsonObject.getString("root_cause_identified");
String solution_implemented = jsonObject.getString("solution_implemented");
String implementaion_date = jsonObject.getString("implementaion_date");
String createdby = jsonObject.getString("createdby");
String updatedby = jsonObject.getString("updatedby");
String created_date = jsonObject.getString("created_date");
String updated_date = jsonObject.getString("updated_date");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
尝试下面的答案,根据您的 json 样本,您正在尝试错误地迭代 'data' 和 'root_cause_identification'
try {
JSONObject res = new JSONObject(response);
if (res.getString("status").equalsIgnoreCase("success")) {
JSONObject obj = res.getJSONObject("data");
JSONArray rootCauseIdentificationArray = obj.getJSONArray("root_cause_identification");
List<RootCauseIdentificationModel> tempList = new ArrayList<>();
for(int i = 0; i<routeCauseIdentificationArray.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"));
tempList.add(model);
}
stepsList.add(new TQMSavedDataModel(obj.getString("tangible_benefits"),
obj.getString("intangible_benefits"),
obj.getString("total_annual_savings"),
tempList));
}
} 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();
}