简体   繁体   中英

How to call a json array which is inside a object and that object is in side a array?

I want to set a nubmer of meetings per day in a recycler view. I am getting JSON object as a Main object which consist of array named data data consist of object which has date along with it it consist of a array which have n number of objects. My question is how to bound date in data array with time in my secondary array.

I had tried to fetch data using volley i am getting the data but whats happening is the last object in time is displaying number of times which is equal to number of object present in time array.

 dataModelArrayList = new ArrayList<>();

   JSONArray dataArray = obj.getJSONArray("data"); 
   for (int i = 0; i < dataArray.length(); i++) {                                    
        playerModel = new VipPojo();              
        JSONObject dataobj = dataArray.getJSONObject(i);
        playerModel.setDate(dataobj.getString("date"));                                    
        JSONArray dataArrays1 = dataobj.getJSONArray("time");                                    
        for (int j = 0; j < dataArrays1.length(); j++) {
            JSONObject dataobj1 = dataArrays1.getJSONObject(j);                                            
            playerModel.setTimes(dataobj1.getString("times"));
            playerModel.setNotice(dataobj1.getString("notice"));
            playerModel.setCategory(dataobj1.getString("category"));
            dataModelArrayList.add(playerModel)
        }
    }
    rvAdapter = new MeetingAdapter(getActivity().getApplicationContext(), dataModelArrayList); 
    recyclerView.setAdapter(rvAdapter);
    recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),, getResources().getInteger(R.integer.number_of_grid_items));

This is my adapter onBindViewHolder

holder.empname.setText(dataModelArrayList.get(position).getDate());
holder.postname.setText(dataModelArrayList.get(position).getTimes());

Here is the JSON

{
    "data": [
        {
            "date": "Thursday 1, August, 2019",
            "time": [
                {
                    "times": "1:13PM to 1:13PM",
                    "notice": "testing",
                    "category": "Meeting"
                },
                {
                    "times": "12:00PM to 1:00PM",
                    "notice": "Meeting",
                    "category": "Meeting"
                }
            ]
        },
        {
            "date": "Friday 2, August, 2019",
            "time": [
                {
                    "times": "3:00PM to 3:30PM",
                    "notice": "Appointment",
                    "category": "Meeting"
                },
                {
                    "times": "12:00PM to 12:30PM",
                    "notice": "Appointment",
                    "category": "Meeting"
                }
            ]
        },
        {
            "date": "Monday 5, August, 2019",
            "time": [
                {
                    "times": "11:00AM to 11:30AM",
                    "notice": "Obj",
                    "category": "Meeting"
                }
            ]
        }
    ],
    "responseCode": 1,
    "responseMessage": ""
}

Expecting to link data with correct time.

Your code walkthrough:

VipPojo playerModel;
try {
    JSONArray dataArray = obj.getJSONArray("data");
    for (int i = 0; i < dataArray.length(); i++) {
        // playerModel = new VipPojo();
        JSONObject dataobj = dataArray.getJSONObject(i);
        JSONArray dataArrays1 = dataobj.getJSONArray("time");
        for (int j = 0; j < dataArrays1.length(); j++) {
            playerModel = new VipPojo(); // initialize here
            JSONObject dataobj1 = dataArrays1.getJSONObject(j);
            playerModel.setDate(dataobj.getString("date"));
            playerModel.setTimes(dataobj1.getString("times"));
            playerModel.setNotice(dataobj1.getString("notice"));
            playerModel.setCategory(dataobj1.getString("category"));
            dataModelArrayList.add(playerModel);
        }
    }
} catch (JSONException e) {
    e.printStackTrace();
}

but your "date" object is adding previously assigned value to your data model since you are using setDate() inside the inner loop which you don't that. that's a little above my paygrade, but until you find a solution you can move "date" object inside time array next to notice , category ,... this will solve it for now.

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