org.json.JSONException:org.json.JSONObject 类型的 idddsss 中的值 {"storeid0":["1535"],"storeid1":["1862"]} 无法转换为 JSONArray

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

我从 api

那里收到了这条回复

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

这是我用来从服务器获取响应并将所有商店 ID 保存在数组中的代码:

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();
            }
        });

但是每当我 运行 代码出现以下错误时,我不知道为什么,我先将我的代码解析为 JSONObject,然后再解析为 JSONArray,然后在循环中我再次将它解析为 JSONObject。

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.onResponse(Payment_fragment.java:356)
        at com.paysano.Fragment.Payment_fragment.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)

idddsss 是对象,所以使用 getJSONObject 代替 getJSONArray

JSONObject objIdddsss = jsonObject.getJSONObject("idddsss");
JSONArray jsonArray = objIdddsss.getJSONArray("storeid0");
Log.v("ANS", objIdddsss.toString());
Log.v("ANS", jsonArray.toString());