【问题标题】:org.json.JSONException:Value[{}] of type org.json.JSONArray cannot be converted to JSONObjectorg.json.JSONException: org.json.JSONArray 类型的Value[{}] 无法转换为 JSONObject
【发布时间】:2018-02-04 14:32:45
【问题描述】:

当我调用我的服务网站时,我遇到了这个异常:org.json.JSONArray 类型的 org.json.JSONException:Value[{}] 无法转换为 JSONObject

result

这是我的源代码:

JsonObjectRequest obreq = new JsonObjectRequest(Request.Method.GET, url + editText.getText().toString(), null,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {

                               try {

                                JSONArray missions = response.getJSONArray("Value");

                                for(int i=0;i<missions.length();i++){
                                        // Get current json object
                                        JSONObject mission = missions.getJSONObject(i);

                                        // Get the current student (json object) data
                                        Long idMission = mission.getLong("idMission");
                                        String etatMission = mission.getString("etatMission");

                                        text.append(idMission+" " + etatMission);
                                        text.append("\n\n");
                                        }
                                }catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                text.setText(error.getMessage());
                            }
                        }
                );
                requestQueue.add(obreq);

【问题讨论】:

    标签: android json rest web-services android-volley


    【解决方案1】:

    如果您从 API 获得的响应是​​ JsonArray,那么您应该使用这个:

    public JsonArrayRequest(int method, String url, JSONObject jsonRequest,
        Listener<JSONArray> listener, ErrorListener errorListener) {
    super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), 
        listener, errorListener);
    }
    

    【讨论】:

      【解决方案2】:

      答案很简单:

      for(int i=0;i<response.length();i++){
                               JSONObject mission = response.getJSONObject(i);
      }
      

      【讨论】:

        猜你喜欢
        • 2017-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        • 2013-12-13
        相关资源
        最近更新 更多