如何在没有外部库的情况下解析包含对象列表的对象列表
Posted
技术标签:
【中文标题】如何在没有外部库的情况下解析包含对象列表的对象列表【英文标题】:How to parse list of objects with list of objects in it without external libraries 【发布时间】:2021-09-24 04:04:03 【问题描述】:我在不使用外部库的情况下练习在 Kotlin 中解析 JSON 字符串,当我有一个包含对象列表的对象列表时问题就开始了。 这是我的 JSON
"id":"1",
"title":"Schedule",
"weekly_activity":[
"day":"Monday",
"activity":[
"start":"07:30",
"end":"",
"activity_name":"Breakfast"
,
"start":"07:30",
"end":"09:30",
"activity_name":"Escursion"
,
"start":"09:30",
"end":"11:00",
"activity_name":"Talk"
,
"start":"11:30",
"end":"15:00",
"activity_name":"Walk"
,
"start":"15:00",
"end":"16:30",
"activity_name":"Lunch"
,
"start":"Dalle",
"end":"17:30",
"activity_name":"Break"
]
,
"day":"Tuesday",
"activity":[
"start":"07:31",
"end":"",
"activity_name":"Breakfast in the forest"
,
"start":"07:31",
"end":"09:31",
"activity_name":"Walk"
,
"start":"09:31",
"end":"11:01",
"activity_name":"Escursion"
,
"start":"11:31",
"end":"15:01",
"activity_name":"Break"
,
"start":"15:01",
"end":"16:31",
"activity_name":"Lunch"
,
"start":"Dalle",
"end":"17:31",
"activity_name":"Free time"
]
]
这是我解析 JSON 的函数:
fun parseString (): Scheduler
val listOfActivity: ArrayList<Activity> = arrayListOf()
val listOfWeeklyActivity: ArrayList<WeeklyActivity> = arrayListOf()
val jsonStringScheduler = JSONObject(parseString)
val id = jsonStringScheduler.getString("id")
val title = jsonStringScheduler.getString("title")
val weeklyActivityList =jsonStringScheduler.getJSONArray("weekly_activity")
weeklyActivityList.let
(0 until it.length()).forEach
val weeklyActivity = weeklyActivityList.getJSONObject(it)
val day = weeklyActivity.getString("day")
val activityList = weeklyActivity.getJSONArray("activity")
activityList.let
(0 until it.length()).forEach
val activity = activityList.getJSONObject(it)
val start = activity.getString("start")
val end = activity.getString("end")
val activityName = activity.getString("activity_name")
listOfActivity.add(Activity(start, end, activityName))
listOfWeeklyActivity.add(WeeklyActivity(day,listOfActivity))
val scheduler = Scheduler(id, title, listOfWeeklyActivity)
return scheduler
每次我尝试像这样解析时,我的“每周活动”对象都会列出这两个活动,但我需要像 JSON 一样拥有它。
【问题讨论】:
您应该为每个活动列表创建新列表。当您尝试解析第二个“每周活动”时,“listOfActicity”已经包含前一个活动。换句话说,它不是空的。 【参考方案1】:您的问题是您只创建一次listOfActivity
,并且您在每次迭代中都继续修改它。相反,您必须为每个weeklyActivity
创建一个新的listOfActivity
。要解决这个问题,您需要将listOfActivity
的声明移动到weeklyActivityList
的循环中,如下所示
fun parseString(): Scheduler
// remove this line ---> val listOfActivity: ArrayList<Activity> = arrayListOf()
...
jsonStringScheduler.getJSONArray("weekly_activity")
weeklyActivityList.let
(0 until it.length()).forEach
// Create new list of activity for every week here
val listOfActivity: ArrayList<Activity> = arrayListOf()
activityList.let
(0 until it.length()).forEach
...
listOfActivity.add(Activity(start, end, activityName))
// listOfActivity for this week is complete, now add it to listOfWeeklyActivity
// And for next WeeklyActivity create new listOfActivity
listOfWeeklyActivity.add(WeeklyActivity(day, listOfActivity))
val scheduler = Scheduler(id, title, listOfWeeklyActivity)
return scheduler
【讨论】:
以上是关于如何在没有外部库的情况下解析包含对象列表的对象列表的主要内容,如果未能解决你的问题,请参考以下文章
如何在不指向 Java 源列表中的对象的情况下复制 ArrayList? [复制]
如何在不使用 Python 中的外部库的情况下解析 arff 文件