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

如何在Android中使用volley通过api检索json中数组的子数组?,android,json,api,android-studio,android-volley,Android,Json,Api,Android Studio,Android Volley,JSON api文件: { "state": "Jammu and Kashmir", "statecode": "JK", "districtData": [ { "district": "Anantnag", "notes": "", "active": 107, "confirmed": 109, "deceased": 1, "recovered": 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
        }
      }
    ]
  }
        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);
Java代码:

  {
    "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
        }
      }
    ]
  }
        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);
我无法获取数据,因为它的格式不正确。我还想使用where子句,它将为我提供我想要的州的每个地区的数据

API链接:

结构
JsonArrayRequest=newjsonarrayrequest(request.Method.GET,”https://api.covid19india.org/v2/state_district_wise.json“,空,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
试一试{
对于(int i=0;i
试试这种方法

 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);
StringRequest StringRequest=新的StringRequest(Request.Method.GET,
"https://api.covid19india.org/v2/state_district_wise.json",
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
Log.d(标签“onResponse:”+响应);
试一试{
JSONArray responseArray=新JSONArray(响应);
对于(int i=0;i对于(int j=0;j响应为
JSONArray
,您正在将其解析为
JSONObject
。请添加logcat.logcat添加。请检查。您找到任何解决方案了吗?它在您的设备上工作吗?现在检查我更新的答案。它在我的设备上工作。@owaisysoufdata未提取。@owaisysouf任何异常?您调试了吗?我我没有任何错误。但是没有加载数据。
progressDialog
没有被取消。@owaisysouf检查我编辑的答案。您应该尝试使用
JsonArrayRequest
不显示数据