Skip to content
Advertisement

org.json.JSONException: Value {“storeid0”:[“1535″],”storeid1”:[“1862”]} at idddsss of type org.json.JSONObject cannot be converted to JSONArray

I am getting this response from my api

{"idddsss":{"storeid0":["1535"],"storeid1":["1862"]}}

this is the code that I am using to get response from server and save all storeids in an array:

public ArrayList<String> tripItems = new ArrayList<>();
    private void getAllStoreIds(String user_id){

        String url = Baseurl.getCartDetails+"?user_id="+user_id;

        StringRequest postStringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, "ResponseCheck :" + response);
                        tripItems.clear();
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            JSONArray json = jsonObject.getJSONArray("idddsss");
                            if(json.length ()> 0){
                                for(int countItem = 0;countItem<json.length();countItem++){
                                    JSONObject o = json.getJSONObject(countItem);
                                }

                                if (!tripItems.equals(null)) {
                                    for (int i = 0; i < tripItems.size(); i++) {
                                        Toast.makeText(myContext, tripItems.get(i), Toast.LENGTH_SHORT).show();
                                    }
                                }

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(myContext, e.getMessage()+ "Error", Toast.LENGTH_SHORT).show();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(myContext, error.getCause().toString(), Toast.LENGTH_SHORT).show();
            }
        });

But whenever i run the code i got following error, I don’t know why, I have parsed my code to JSONObject first and then to JSONArray and in loop I am parsing it to JSONObject again.

W/System.err: org.json.JSONException: Value {"storeid0":["1535"],"storeid1":["1862"]} at idddsss of type org.json.JSONObject cannot be converted to JSONArray
W/System.err:     at org.json.JSON.typeMismatch(JSON.java:101)
        at org.json.JSONObject.getJSONArray(JSONObject.java:598)
        at com.paysano.Fragment.Payment_fragment$6.onResponse(Payment_fragment.java:356)
        at com.paysano.Fragment.Payment_fragment$6.onResponse(Payment_fragment.java:349)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:227)
        at android.app.ActivityThread.main(ActivityThread.java:7822)
        at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1026)

Advertisement

Answer

idddsss is object, so use getJSONObject instead getJSONArray

JSONObject objIdddsss = jsonObject.getJSONObject("idddsss");
JSONArray jsonArray = objIdddsss.getJSONArray("storeid0");
Log.v("ANS", objIdddsss.toString());
Log.v("ANS", jsonArray.toString());
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement