如何使用Volley库解析嵌套的json数组?

时间:2019-06-10 06:30:22

标签: android json android-volley

我是android新手。我不知道这种类型的解析。我想绑定这个   在Recyclerview

回复Json

{
    "error": true,
    "callcount": {
        "1": {
            "count": 1,
            "name": "Vipul Dusane"
        },
        "2": {
            "count": 0,
            "name": "Aniket Patil"
        }
    },
    "success": "true",
    "message": "Record Found!"
}

排球功能

 private void ShowCsllCount() {

    StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.TotalCount ,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        progressDialog.dismissWithAnimation();

                        JSONObject obj = new JSONObject(response);

                        JSONArray dataArray  = obj.getJSONArray("callcount");

                        for (int i = 0; i < dataArray.length(); i++) {
                            JSONObject heroObject = dataArray.getJSONObject(i);

                            totalC_Pojo totalCPojo = new totalC_Pojo
                                    (heroObject.getString("count"),heroObject.getString("name"));

                            dataModelArrayList.add(totalCPojo);
                        }

                        totalCountAd = new TotalCountAd(OtherWR.this, dataModelArrayList);
                        recycler.setAdapter(totalCountAd);
                    } catch (JSONException e) {
                        e.printStackTrace();
                        progressDialog.dismissWithAnimation();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //displaying the error in toast if occurrs
                    progressDialog.dismissWithAnimation();

                    Toast.makeText(OtherWR.this, error.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
    // request queue
    RequestQueue requestQueue = Volley.newRequestQueue(OtherWR.this);
    requestQueue.add(stringRequest);

2 个答案:

答案 0 :(得分:0)

json响应不正确。

使用此回复

{
    "error": true,
    "callcount": [
        "1": {
            "count": 1,
            "name": "Vipul Dusane"
        },
        "2": {
            "count": 0,
            "name": "Aniket Patil"
        }
    ],
    "success": "true",
    "message": "Record Found!"
}

代替

{
    "error": true,
    "callcount": {
        "1": {
            "count": 1,
            "name": "Vipul Dusane"
        },
        "2": {
            "count": 0,
            "name": "Aniket Patil"
        }
    },
    "success": "true",
    "message": "Record Found!"
}

答案 1 :(得分:0)

尝试一下...

JSONObject callcountObj = new JSONObject(response).getJSONObject("callcount");

JSONObject callcount1 = callcountObj.getJSONObject("1");

JSONObject callcount2 = callcountObj.getJSONObject("2");

//callcount one 
String callcount1_count = callcount1.getString("count");
String callcount1_name = callcount1.getString("name");

//callcount two
String callcount2_count = callcount2.getString("count");
String callcount2_name = callcount2.getString("name");