at data of type org.json.JSONObject cannot be converted to JSONArray

和自甴很熟 提交于 2020-01-25 10:13:51

问题


my response

{
    "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 code

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();
        }

回答1:


Please try below code:

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();
        }



回答2:


Try this below answer, above as per your json sample you are trying to iterate 'data' and 'root_cause_identification' wrongly

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();
}



回答3:


The response you're getting is a JSONObject not a JSONArray.




回答4:


First, you should get data as JsonObject Then, from this object, you can retrieve the array you need :)

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();
        }

Happy coding !




回答5:


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();
        }



回答6:


This is the right answer.

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();
        }


来源:https://stackoverflow.com/questions/59820204/at-data-of-type-org-json-jsonobject-cannot-be-converted-to-jsonarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!