【发布时间】: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