简体   繁体   English

at 类型 org.json.JSONObject 的数据无法转换为 JSONArray

[英]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",
        "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();
        }

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

您得到的响应是JSONObject而不是JSONArray

Try this below answer, above as per your json sample you are trying to iterate 'data' and 'root_cause_identification' wrongly试试下面的答案,上面根据你的 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();
}

First, you should get data as JsonObject Then, from this object, you can retrieve the array you need :)首先,你应该以 JsonObject 的形式获取data然后,从这个对象中,你可以检索你需要的数组:)

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 !快乐编码!

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将org.json.JSONObject类型的数据转换为JSONArray - Data of type org.json.JSONObject cannot be converted to JSONArray 类型为org.json.JSONObject的响应无法转换为JSONArray - at response of type org.json.JSONObject cannot be converted to JSONArray 值类型 org.json.JSONObject 无法转换为 JSONArray - Value type org.json.JSONObject cannot be converted to JSONArray org.json.JSONObject无法转换为JSONArray - org.json.JSONObject cannot be converted to JSONArray 错误org.json.JSONObject无法转换为JSONArray - error org.json.JSONObject cannot be converted to JSONArray 解析JSON数组时,获取异常“ org.json.JSONObject类型的答案无法转换为JSONArray” - Getting exception “ at answer of type org.json.JSONObject cannot be converted to JSONArray” while parsing JSON array 在Android中解析JSON响应“无法将org.json.JSONObject类型的响应转换为JSONArray” - Parse JSON response in android “response of type org.json.JSONObject cannot be converted to JSONArray” 我有一个错误:类型 org.json.JSONObject 无法转换为 JSONArray - I have an error : type org.json.JSONObject cannot be converted to JSONArray org.json.JSONException:值 {“storeid0”:[“1535”],“storeid1”:[“1862”]} 类型为 org.json.JSONObject 的 idddsss 无法转换为 JSONArray - org.json.JSONException: Value {“storeid0”:[“1535”],“storeid1”:[“1862”]} at idddsss of type org.json.JSONObject cannot be converted to JSONArray 无法将类型为org.json.JSONObject $ 1的值null转换为JSONObject错误 - Value null of type org.json.JSONObject$1 cannot be converted to JSONObject error found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM