如何在Android中使用volley通过api检索json中数组的子数组?

问题描述 投票:0回答:1

JSON api文件。

  {
    "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代码。

        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): 下面是我在运行我的应用时得到的日志猫。

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/[email protected] 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.

我无法获取数据,因为它的格式不正确。我想使用where子句,这将给我一个州的每个地区的数据,我想。

API链接。 https:/api.covid19india.orgv2state_district_wise.json。

android json api android-studio android-volley
1个回答
1
投票

結構

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

1
投票

试试这样

 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);
© www.soinside.com 2019 - 2024. All rights reserved.