我的错误是JSONArray无法转换为JSONObject

时间:2018-05-07 08:16:41

标签: android

我是JSON的新手,我收到以下例外:

org.json.JSONArray cannot be converted to JSONObject在try部分的第一行中。

请帮我删除。这是我的代码:

public void sendjsonrequest(){
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray jsonArray=response.getJSONArray("dish");
                    for(int i=0; i<jsonArray.length();i++) {
                     JSONObject dishes = jsonArray.getJSONObject(i);
                      int  idd = dishes.getInt("id");
                      String  latitude = dishes.getString("latitude");
                      String  longitude = dishes.getString("longitude");
                     String   snip = dishes.getString("snippet");
                        id.append(idd+","+latitude+","+longitude+","+snip+"");                      //  id.setText(idd);
                      //  lat.setText(latitude);
                      //  lon.setText(longitude);
                      //  snippet.setText(snip);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },new Response.ErrorListener(){

            @Override
            public void onErrorResponse(VolleyError error) {
                 error.printStackTrace();
            }
        });

        rq.add(jsonObjectRequest);
    }

这是我从我的php文件中获取的json

[{"id":"3","latitude":"Latitude: 11.913859799999999","longitude":"Longitude: 79.8144722","snippet":"I am Here"}]

1 个答案:

答案 0 :(得分:0)

试试这段代码:

将参数中的响应更改为JSONArray而不是JSONObject。 将 JsonObjectRequest 更改为JsonArrayRequest

你的JSON:

[{"id":"3","latitude":"Latitude: 11.913859799999999","longitude":"Longitude: 79.8144722","snippet":"I am Here"}]

代码:

    public void sendjsonrequest(){

        JsonArrayRequest jsonObjectRequest=new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONArray jsonArray) { // Change it here
                try {
                    for(int i=0; i<jsonArray.length();i++) {
                        JSONObject dishes = jsonArray.getJSONObject(i);
                        int  idd = dishes.getInt("id");
                        String  latitude = dishes.getDouble("latitude") +"";
                        String  longitude = dishes.getDouble("longitude") + "";
                        String   snip = dishes.getString("snippet");
                        id.append(idd+","+latitude+","+longitude+","+snip+"");                      //  id.setText(idd);
                        //  lat.setText(latitude);
                        //  lon.setText(longitude);
                        //  snippet.setText(snip);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },new Response.ErrorListener(){

            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        rq.add(jsonObjectRequest);
    }