gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:55:43 25 4
gpt4 key购买 nike

请查看这个 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() 正在内部循环中使用,它我不想要那个。

你能帮我解决这个问题吗?

最佳答案

关于android - 遍历嵌套的 json 数组是将先前分配的值添加到数据模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57433666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com