【发布时间】:2019-12-20 12:30:09
【问题描述】:
我有 json
{
"message": null,
"data": {
"Commodity Department": {
"total": 2,
"completed": 1,
"completedWithDue": 0,
"completedWithOutDue": 1,
"inProgress": 1,
"inProgressWithDue": 0,
"inProgressWithOutDue": 1,
"statusCounter": null
}
}
我需要将每个部门的 json 对象转换为数组。目前每个类别值("total": 0, "completed": 0, "completedWithDue": 0, "completedWithOutDue": 0, "inProgress": 0, "inProgressWithDue": 0, "inProgressWithOutDue": 0,) 将采用对象格式。我需要转换数组并根据类别加载到collectionview。截至目前,我正在尝试在下面的代码中解码我的 json
public struct Dashboard: Decodable {
public let data : [String:Departments]
}
public struct Departments: Decodable {
public let total, completed, completedWithDue, completedWithOutDue: Int
public let inProgress, inProgressWithDue, inProgressWithOutDue: Int
}
let dashboard = try? JSONDecoder().decode(Dashboard.self, from: response.data!)
print(dashboard!.data.keys)
【问题讨论】:
-
问题是什么?
-
@vadian 当前每个类别的值(“total”:0,“completed”:0,“completedWithDue”:0,“completedWithOutDue”:0,“inProgress”:0,“inProgressWithDue”:0 , "inProgressWithOutDue": 0,) 将是对象格式。我需要根据类别将数组加载转换为collectionview
-
为了便于阅读,请将代码添加到您的问题中,而不是在 cmets 中。另外,
print(dashboard!.data.keys)的输出是什么? -
这能回答你的问题吗? How to parse Array of JSON to array in Swift
-
@koen 我得到了部门名称 All Departments, Portfolio Mangement Service.....