循环遍历嵌套json数组将先前分配的值添加到数据模型

时间:2019-08-09 16:01:27

标签: android json arraylist android-json

请查看此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"
        }
      ]
    }
  ]
}

我像这样将json数据拖到我的ArrayList中:

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();
}
正如我在上面的代码的注释中提到的那样,

对象"date"正在向数据模型添加先前分配的值,因为setDate()在内部循环中被使用,我不希望那样。

您能帮我吗?

4 个答案:

答案 0 :(得分:1)

使用Gson库Get nested JSON object with GSON using retrofit

对于嵌套对象,请在其自身中使用一个对象

有关更多信息,https://futurestud.io/tutorials/gson-mapping-of-nested-objects

答案 1 :(得分:0)

更改 playerModel.setDate(dataobj.getString("date"));playerModel.setDate(date);

答案 2 :(得分:0)

在VipPojo中“日期”字段不是静态的

答案 3 :(得分:0)

您可以尝试

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