【发布时间】:2019-12-21 11:18:12
【问题描述】:
我的场景,我正在尝试将JSON 数据加载到UITableView。在这里,问题是我的 JSON 有多个 array 和多个 values。我需要将数组 keys 作为 Tableview section 名称并将其全部 values 加载到相关的 cell 中。我正在使用codable 方法进行简单的 JSON 数据处理。现在,如何将数组键名(学校、办公室等)放入部分及其值相关的单元格中。
我的 JSON
https://api.myjson.com/bins/r763z
我的可编码
struct Root : Decodable {
let status : Bool
let data: ResultData
}
struct ResultData : Decodable {
let school, college, office, organisation, central : [Result]
}
struct Result : Decodable {
let id, name, date : String
let group : [String]
}
我的 JSON 解码器代码
func loadJSON(){
let urlPath = "https://api.myjson.com/bins/r763z"
let url = NSURL(string: urlPath)
let session = URLSession.shared
let task = session.dataTask(with: url! as URL) { data, response, error in
guard data != nil && error == nil else {
print(error!.localizedDescription)
return
}
do {
let decoder = JSONDecoder()
self.tableData = try decoder.decode(DivisionData.self, from: data!) // How to get section values and cell values and load table data
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch { print(error) }
}
task.resume()
}
【问题讨论】:
-
@vadian 你能帮忙吗?