【问题标题】:How to parse nested json array using Volley library?如何使用 Volley 库解析嵌套的 json 数组?
【发布时间】:2019-06-10 06:30:22
【问题描述】:

我是安卓新手。我不知道这种类型的解析。我想绑定这个 在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);

【问题讨论】:

  • 使用库解析 Json 比手动编写代码要好。 GSON 是 google 支持的流行的 Json 序列化库。更多信息在这里:github.com/google/gson
  • 通过查看您的 JSON,callcount 是 JSONObject,而不是 JSONArray,但它应该是与您的代码一起使用的数组。
  • 使用嵌套的 pojo 然后用 gson 解析就可以了

标签: android json android-volley


【解决方案1】:

试试这个...

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

【讨论】:

    【解决方案2】:

    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!"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多