我如何从数组Android Studio JSONArray中的数组获取数据

时间:2019-03-26 04:14:07

标签: java android arraylist jsonobjectrequest

我从API作为JSONarray获得响应,我可以将数据输入到我的arraymodel中,但是问题是我需要来自一个数组的一些特定数据,我不知道该怎么做。

该数组是:

"history": [
        {
            "id": "20",
            "code": "mcw_5b97de588ce0c",
            "date": "2018-09-11 17:25:12",
            "status": "1",
            "name": "a:1:{s:7:\"english\";s:9:\"rent bill\";}",
            "img": "shop.png"
        }

我要从姓名中获取英文,S:9和房租数据。

我将代码发布在这里,以便您了解我做了什么以及我犯了什么错误。

public void  shopListing()
{

    showSimpleProgressDialog(this, "Loading...","Fetching Shops History",false);

    StringRequest stringRequest = new StringRequest(Request.Method.POST, shopurl,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    Log.d("strrrrr", ">>" + response);

                    try {

                        JSONObject obj = new JSONObject(response);
                        //if(obj.optString("status").equals("true")){

                        dataModelArrayList = new ArrayList<>();
                        JSONArray dataArray  = obj.getJSONArray("history");

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

                            shopModel playerModel = new shopModel();
                            JSONObject dataobj = dataArray.getJSONObject(i);
                            playerModel.setId(dataobj.getString("id"));
                            playerModel.setName(dataobj.getString("name"));
                            playerModel.setDetails(dataobj.getString("date"));


                            dataModelArrayList.add(playerModel);

                        }

                        setupListview();

                        // }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }

            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //displaying the error in toast if occurrs
                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {

            String api = getApi_key();
            String user = getUser_key();

            Map<String, String> param = new HashMap<> ();
            param.put("api_key", api);
            param.put("user_key", user);


            return param;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    requestQueue.add(stringRequest);

}

1 个答案:

答案 0 :(得分:0)

您的服务器响应不佳。而不是将name作为字符串发送给您,它应该将“ name”作为这样的对象返回:

"history": [
    {
        "id": "20",
        "code": "mcw_5b97de588ce0c",
        "date": "2018-09-11 17:25:12",
        "status": "1",
        "name": 
            {
                "s":7,
                "subject":"english",
                "s":9,
                "any_key":"rent bill"
            },
        "img": "shop.png"
    }

我觉得服务器正在向您返回您编写的用于处理来自客户端的请求的代码字符串。请仔细检查。