1

please look into this 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"
        }
      ]
    }
  ]
}

I'm polulating this json data to my ArrayList like this:

VipPojo playerModel;
try {
    JSONArray dataArray = obj.getJSONArray("data");
    for (int i = 0; i < dataArray.length(); i++) {
        JSONObject dataobj = dataArray.getJSONObject(i);
        JSONArray dataArrays1 = dataobj.getJSONArray("time");
        String date = dataobj.getString("date");
        System.out.println("date: " + date); // here I get correct data (all 3 items);
        for (int j = 0; j < dataArrays1.length(); j++) {
            playerModel = new VipPojo();
            JSONObject dataobj1 = dataArrays1.getJSONObject(j);
            playerModel.setDate(date); // this is adding same previous assigned "date" until loop ends(I get 5 data with duplicate values)
            playerModel.setTimes(dataobj1.getString("times"));
            playerModel.setNotice(dataobj1.getString("notice"));
            playerModel.setCategory(dataobj1.getString("category"));
            dataModelArrayList.add(playerModel);
        }
    }
} catch (JSONException e) {
    e.printStackTrace();
}

as I mentioned in comments in code above, "date" object is adding previously assigned value to data model since setDate() is being used inside the inner loop which I don't want that.

Can you help me with this please?

Majid
  • 201
  • 2
  • 14

4 Answers4

1

use Gson library Get nested JSON object with GSON using retrofit

for nested object use an object in itself

for more information https://futurestud.io/tutorials/gson-mapping-of-nested-objects

amir.ashrafi
  • 107
  • 2
  • 11
0

Change playerModel.setDate(dataobj.getString("date")); to playerModel.setDate(date);

asim
  • 533
  • 6
  • 17
  • I know. it should be `dataobj `. `"date"` object should be assigned with outer array as the json data shows. – Majid Aug 09 '19 at 16:18
0

isn't "date" field static in VipPojo

taha
  • 731
  • 2
  • 7
  • 18
  • 1
    what's the difference, model object is initializing in this loop anyway. yet I've tried that and it didn't worked. – Majid Aug 10 '19 at 04:36
  • I print dataModelArrayList: for (VipPojo vp: dataModelArrayList) { Log.d("gggggg", vp.date); } – taha Aug 10 '19 at 19:33
  • result: Thursday 1, August, 2019 Thursday 1, August, 2019 Friday 2, August, 2019 Friday 2, August, 2019 Monday 5, August, 2019 – taha Aug 10 '19 at 19:33
0

you can try like this

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");
        String date = dataobj.getString("date");
        System.out.println("date: " + date); // here I get correct data (all 3 items);
        playerModel.setDate(date);
        for (int j = 0; j < dataArrays1.length(); j++) {

            JSONObject dataobj1 = dataArrays1.getJSONObject(j);
             // this is adding same previous assigned "date" until loop ends(I get 5 data with duplicate values)
            playerModel.setTimes(dataobj1.getString("times"));
            playerModel.setNotice(dataobj1.getString("notice"));
            playerModel.setCategory(dataobj1.getString("category"));
            dataModelArrayList.add(playerModel);
        }
    }
} catch (JSONException e) {
    e.printStackTrace();
}
KIRAN CSN
  • 121
  • 9