Как разобрать вложенный массив JSON с помощью библиотеки Volley? - PullRequest
0 голосов
/ 10 июня 2019

Я новичок в 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 голосов
/ 11 июля 2019

попробуйте это ...

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");
0 голосов
/ 10 июня 2019

Неправильный ответ 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!"
}
...