简体   繁体   中英

How can i get the data from array inside an array android studio JSONArray

I am getting response from API as a JSONarray and I can get the data into my arraymodel but the problem is I need some specific data from one array and I don't have any idea how to do that.

the array is:

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

I want the English, S:9 and the rent bill data from name.

I am posting my code here so you can understand what I have done and what mistake i'm committing.

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

}

Your server response is not good. instead of sending name as a string to you, it should return you "name" as an object like this:

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

I feel like server is returning you code string which you have written to process request from client. Please double check.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM