org.json.JSONException:org.json.JSONArray类型的值[{}]无法转换为JSONObject

时间:2017-08-26 22:11:31

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

当我调用我的服务网时,我有这个异常:org.json.JSONException:org.json.JSONArray类型的值[{}]无法转换为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);

2 个答案:

答案 0 :(得分:0)

如果您从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);
}

答案 1 :(得分:0)

ansewer非常简单:

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