简体   繁体   中英

Value Array of type java.lang.String cannot be converted to JSONObject

I have started android programming and recently getting couple of issues. I am fetching data from database and using POSTMAN, i can see the result in JSON like below but while parsing it, its giving me error:

{
  "result": [
    {
      "Date": "18-3-2016",
      "Events": "Local Holiday"
    },
    {
      "Date": "23-3-2016",
      "Events": "Monthly Fees"
    },
    {
      "Date": "15-4-2016",
      "Events": "Monthly Fees"
    },
    {
      "Date": "23-4-2016",
      "Events": "Annual Day"
    },
    {
      "Date": "30-4-2016",
      "Events": "session end"
    },
    {
      "Date": "9-4-2016",
      "Events": "Parent Teacher Meeting"
    }
  ]
}

I am using code to display but i am getting error, My code is :

private void showJSON(String response) {

    String child_name = "";
    String Date ="";
    String address="";
    //String vc = "";
    try {
        final String TAG = events.class.getSimpleName();
        Log.d(TAG, "showJSON: \n"+response);
        JSONObject jsonObject = new JSONObject(response);

        //Get the instance of JSONArray that contains JSONObjects
       // JSONArray jsonArray = jsonRootObject.optJSONArray(config_events.JSON_ARRAY);
        JSONArray result = jsonObject.getJSONArray(config_events.JSON_ARRAY);
        //Iterate the jsonArray and print the info of JSONObjects
        for (int i = 0; i < result.length(); i++) {
            //JSONObject jsonObject = jsonArray.getJSONObject(i);
            //JSONObject jsonObject = new JSONObject(response);

            Log.d(TAG, "showJSON: Event list size: "+result.length());
            JSONObject collegeData = result.getJSONObject(0);
            child_name = collegeData.getString(config_events.KEY_NAME);
            address = collegeData.getString(config_events.KEY_ADDRESS);
            //vc = collegeData.getString(config.KEY_VC);


            textViewResult.setText("EVENTS:\t" + address);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}

Error:

Value Array of type java.lang.String cannot be converted to JSONObject
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:158)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:171)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at com.weavearound.schools.weavearound.events.showJSON(events.java:105)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at com.weavearound.schools.weavearound.events.access$100(events.java:25)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at com.weavearound.schools.weavearound.events$1.onResponse(events.java:82)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at com.weavearound.schools.weavearound.events$1.onResponse(events.java:78)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:67)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at android.os.Handler.handleCallback(Handler.java:605)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:92)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err:     at android.os.Looper.loop(Looper.java:137)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:4517)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err:     at java.lang.reflect.Method.invoke(Method.java:511)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err:     at dalvik.system.NativeStart.main(Native Method)

Code for response:

String url = config_events.DATA_URL;

        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                loading.dismiss();
                showJSON(response);
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(events.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest

)

You are making String request. Make a JsonObjectRequest instead. Then you'll receive a json response like this -

showJSON(JsonObject response)

Then take out the result from it like this -

JSONArray result = response.getJSONArray("result");

Can you change those lines ...to be sure

            JSONArray result = jsonObject.getJSONArray("result");

            for (int i = 0; i < result.length(); i++) {

                JSONObject collegeData = result.getJSONObject(i);
                child_name = collegeData.getString("Date");
                address = collegeData.getString("Events");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM