0

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
  • 4
    Your data is actual nested Object, not Array Commented Jan 20, 2020 at 9:25

6 Answers 6

4

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();
        }
Sign up to request clarification or add additional context in comments.

1 Comment

i have a model, which is contains, data of tangible_benefits,intangible_benefits,total_annual_savings,root_cause_identification. thats why i using List<RootCauseIdentificationModel>
1

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

Comments

1

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

14 Comments

JSONObject obj=res.getJSONArray("data") error in this line
now my app crash and error is that java.lang.ClassCastException: org.json.JSONObject cannot be cast to java.util.List this error comes on (List<RootCauseIdentificationModel>) rootCauseIdentificationArray.getJSONObject(0)
Now this is your task. rootCauseIdentificationArray.getJSONObject(0) is an object you cannot cast it as List<RootCauseIdentificationModel>....so try to create the list with the iterated data from the array of objects
private ArrayList<RootCauseIdentificationModel> dataList = new ArrayList(); its done , but error are still exist
public RootCauseIdentificationModel(String id,String rootCauseIdentified, String solutionImplemented,String date){ this.id=id; this.rootCauseIdentified=rootCauseIdentified; this.solutionImplemented=solutionImplemented; this.date=date; }
|
0

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 !

4 Comments

I edited my answer, but keep in mind that the data you are retrieving from JSON is NOT part of an array.. tangible_benefits, intangible_benefits , total_annual_savings are shown only once in the JSON. They are NOT part of an array :)
obj.getJSONObject("root_cause_identification") this is an array of objects
please keep in mind, i am using two model in this 1st model is TQMSavedDataModel which contain tangible_benefits,intangible_benefits ,total_annual_savings and RootCauseIdentificationModel . 2nd model is the RootCauseIdentificationModel which contain id, root_cause_identified,solution_implemented,implementaion_date
'root_cause_identification' this keyname at multiple location with multiple types?
0
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 Comments

obj.getJSONObject("root_cause_identification") this is an array of objects
obj , not found
now my app crash and error is that java.lang.ClassCastException: org.json.JSONObject cannot be cast to java.util.List this error comes on (List<RootCauseIdentificationModel>) rootCauseIdentificationArray.getJSONObject(0)
you need to debug it, the major problem I highlighted
and it's not a LIst<RootCauseIdentificationModel> you getting, you have to iterate the array and unmarshal for the bean and add to List.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.