At Data Of Type Org.json.jsonobject Cannot Be Converted To Jsonarray
my response { 'status': 'success', 'statuscode': 200, 'message': 'Record found successfully.', 'data': { 'tangible_benefits': 'ds1351gsghsdh353535535',
Solution 1:
Please try below code:
try {
JSONObjectres=newJSONObject(response);
if (res.getString("status").equalsIgnoreCase("success")) {
JSONObjectTQMData= res.getJSONObject("data");
Stringtangible_benefits= TQMData.getString("tangible_benefits");
Stringintangible_benefits= TQMData.getString("intangible_benefits");
Stringtotal_annual_savings= TQMData.getString("total_annual_savings");
JSONArrayroot_cause_identification= TQMData.getJSONArray("root_cause_identification");
for (inti=0; i < root_cause_identification.length(); i++) {
JSONObjectjsonObject= root_cause_identification.getJSONObject(i);
Stringid= jsonObject.getString("id");
Stringprojectid= jsonObject.getString("projectid");
Stringstep= jsonObject.getString("step");
Stringroot_cause_identified= jsonObject.getString("root_cause_identified");
Stringsolution_implemented= jsonObject.getString("solution_implemented");
Stringimplementaion_date= jsonObject.getString("implementaion_date");
Stringcreatedby= jsonObject.getString("createdby");
Stringupdatedby= jsonObject.getString("updatedby");
Stringcreated_date= jsonObject.getString("created_date");
Stringupdated_date= jsonObject.getString("updated_date");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Solution 2:
The response you're getting is a JSONObject
not a JSONArray
.
Solution 3:
Try this below answer, above as per your json sample you are trying to iterate 'data' and 'root_cause_identification' wrongly
try {
JSONObject res = newJSONObject(response);
if (res.getString("status").equalsIgnoreCase("success")) {
JSONObject obj = res.getJSONObject("data");
JSONArray rootCauseIdentificationArray = obj.getJSONArray("root_cause_identification");
List<RootCauseIdentificationModel> tempList = newArrayList<>();
for(int i = 0; i<routeCauseIdentificationArray.length(); i++){
JSONObject objData = rootCauseIdentificationArray.getJSONObject(i);
//iterate the object here and add to the listRootCauseIdentificationModel model = newRootCauseIdentificationModel(objData.getString("id"), objData.getString("root_cause_identified"), objData.getString("solution_implemented"), objData.getString("implementaion_date"));
tempList.add(model);
}
stepsList.add(newTQMSavedDataModel(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();
}
Solution 4:
First, you should get data
as JsonObject
Then, from this object, you can retrieve the array you need :)
try {
JSONObject res = newJSONObject(response);
if (res.getString("status").equalsIgnoreCase("success")) {
JSONObjectTQMData = 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(newTQMSavedDataModel(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();
}
Happy coding !
Solution 5:
try {
JSONObject res = newJSONObject(response);
if (res.getString("status").equalsIgnoreCase("success")) {
JSONObjectTQMData = 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();
}
Post a Comment for "At Data Of Type Org.json.jsonobject Cannot Be Converted To Jsonarray"