How to retrieve sub-array of an array in json through api using volley in Android?

此生再无相见时 提交于 2021-02-11 07:09:40

问题


JSON api file:

  {
    "state": "Jammu and Kashmir",
    "statecode": "JK",
    "districtData": [
      {
        "district": "Anantnag",
        "notes": "",
        "active": 107,
        "confirmed": 109,
        "deceased": 1,
        "recovered": 1,
        "delta": {
          "confirmed": 0,
          "deceased": 0,
          "recovered": 0
        }
      },
      {
        "district": "Budgam",
        "notes": "",
        "active": 18,
        "confirmed": 30,
        "deceased": 0,
        "recovered": 12,
        "delta": {
          "confirmed": 4,
          "deceased": 0,
          "recovered": 0
        }
      }
    ]
  }

Java Code:

        StringRequest stringRequest = new StringRequest(Request.Method.GET,
                "https://api.covid19india.org/v2/state_district_wise.json",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            JSONArray jsonArray = jsonObject.getJSONArray("districtData");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject object = jsonArray.getJSONObject(i);
                                // Need to get data here !!!
                            }
                            progressDialog.cancel();
                        } catch (Exception e) {
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);

Logcat (debug): Down below is the logcat which I am getting when trying to run my app.

2020-05-05 16:21:49.663 8813-8813/? I/owais.wabah202: Late-enabling -Xcheck:jni
2020-05-05 16:21:50.013 8813-8813/com.johnowais.wabah2020 I/Perf: Connecting to perf service.
2020-05-05 16:21:50.160 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden method Landroid/graphics/drawable/Drawable;->getOpticalInsets()Landroid/graphics/Insets; (light greylist, linking)
2020-05-05 16:21:50.160 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden field Landroid/graphics/Insets;->left:I (light greylist, linking)
2020-05-05 16:21:50.160 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden field Landroid/graphics/Insets;->right:I (light greylist, linking)
2020-05-05 16:21:50.160 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden field Landroid/graphics/Insets;->top:I (light greylist, linking)
2020-05-05 16:21:50.160 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden field Landroid/graphics/Insets;->bottom:I (light greylist, linking)
2020-05-05 16:21:50.235 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2020-05-05 16:21:50.239 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2020-05-05 16:21:50.289 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden method Landroid/widget/TextView;->getTextDirectionHeuristic()Landroid/text/TextDirectionHeuristic; (light greylist, linking)
2020-05-05 16:21:50.389 8813-8813/com.johnowais.wabah2020 D/OpenGLRenderer: Skia GL Pipeline
2020-05-05 16:21:50.495 8813-8856/com.johnowais.wabah2020 I/Adreno: QUALCOMM build                   : 2df12b3, I07da2d9908
    Build Date                       : 10/04/18
    OpenGL ES Shader Compiler Version: EV031.25.03.01
    Local Branch                     : 
    Remote Branch                    : 
    Remote Branch                    : 
    Reconstruct Branch               : 
2020-05-05 16:21:50.495 8813-8856/com.johnowais.wabah2020 I/Adreno: Build Config                     : S L 6.0.7 AArch64
2020-05-05 16:21:50.495 8813-8856/com.johnowais.wabah2020 D/vndksupport: Loading /vendor/lib64/hw/gralloc.msm8953.so from current namespace instead of sphal namespace.
2020-05-05 16:21:50.502 8813-8856/com.johnowais.wabah2020 I/Adreno: PFP: 0x005ff087, ME: 0x005ff063
2020-05-05 16:21:50.509 8813-8856/com.johnowais.wabah2020 I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2020-05-05 16:21:50.509 8813-8856/com.johnowais.wabah2020 I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2020-05-05 16:21:50.511 8813-8856/com.johnowais.wabah2020 I/OpenGLRenderer: Initialized EGL, version 1.4
2020-05-05 16:21:50.511 8813-8856/com.johnowais.wabah2020 D/OpenGLRenderer: Swap behavior 2
2020-05-05 16:21:50.513 8813-8858/com.johnowais.wabah2020 D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2020-05-05 16:21:50.515 8813-8858/com.johnowais.wabah2020 I/DpmTcmClient: RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor
2020-05-05 16:21:50.565 8813-8856/com.johnowais.wabah2020 D/vndksupport: Loading /vendor/lib64/hw/android.hardware.graphics.mapper@2.0-impl.so from current namespace instead of sphal namespace.
2020-05-05 16:21:50.566 8813-8856/com.johnowais.wabah2020 D/vndksupport: Loading /vendor/lib64/hw/gralloc.msm8953.so from current namespace instead of sphal namespace.

I am not able to fetch data as it is not in proper format. I want to use where clause also which will give me data of each district of a state which I want.

API link: https://api.covid19india.org/v2/state_district_wise.json


回答1:


STRUCTURE

   JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, "https://api.covid19india.org/v2/state_district_wise.json",null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {


                    try {

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

                            JSONObject dataOBJ = response.getJSONObject(i);
                            JSONArray jsonChild = dataOBJ.getJSONArray("districtData");


                            for (int k = 0; k < jsonChild.length(); k++) {

                                JSONObject obj =  jsonChild.getJSONObject(k);
                                int active = obj.getInt("active");
                                String district = obj.getString("district");

                                JSONObject child = obj.getJSONObject("delta");
                                int confirmed = child.getInt("confirmed");


                            }


                        }

                    } catch (Exception exp) {

                    }



                }

            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(request);



回答2:


Try this way

 StringRequest stringRequest = new StringRequest(Request.Method.GET,
            "https://api.covid19india.org/v2/state_district_wise.json",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.d(TAG, "onResponse: "+response);
                    try {

                        JSONArray responseArray = new JSONArray(response);
                        for (int i = 0; i < responseArray.length(); i++) {
                            JSONObject stateObject = responseArray.getJSONObject(i);

                            JSONArray districtArray = stateObject.getJSONArray("districtData");

                            for(int j=0; j<districtArray.length(); j++){
                                JSONObject districtObject = districtArray.getJSONObject(j);
                                //do what you want to do with district object
                                Log.d(TAG, "onResponse: "+districtObject.toString());
                            }
                        }
                    }catch (JSONException e){
                        Log.d(TAG, "onResponse: "+e.getMessage());
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);


来源:https://stackoverflow.com/questions/61611054/how-to-retrieve-sub-array-of-an-array-in-json-through-api-using-volley-in-androi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!