我正在处理这个。这是mi JSON响应
{
"code": 200,
"message": "ok",
"data": {
"section1": [
{
"clave": "xxxx",
"orden": 0,
"nombre": "xxxxx",
"video": "xxxxx",
"imagen": "xxxxx",
"series": 0,
"repeticiones": 0,
"descanso":0,
"completado": false
},
{
"clave": "xxxx",
"orden": 0,
"nombre": "xxxxx",
"video": "xxxxx",
"imagen": "xxxxx",
"series": 0,
"repeticiones": 0,
"descanso":0,
"completado": false
}
}
],
"section2": [
{
"clave": "xxx",
"equipo": "xx",
"imagen": "x",
"tiempo": 0,
"intensidad": 0,
"completado": false
}
],
"section3": [
{
"clave": "xxx",
"nombre": "xxxx",
"imagen": "",
"completado": false
},
{
"clave": "xxx",
"nombre": "xxxx",
"imagen": "",
"completado": false
}
],
"section4": [
{
"clave": "xx",
"nombre": "xxxx",
"imagen": "x",
"completado": false
},
{
"clave": "xx",
"nombre": "xxxx",
"imagen": "x",
"completado": false
}
]
}
}
我要做的是在各节中显示信息,这些节应该是“ section1”,“ section2”,“ section3”,“ section4”,并且显然要显示“ section1”包含的所有信息,如果section是“ section2”,显示cardios中的所有信息,以此类推...但是我想在同一tableView中显示它,只是分成几部分,您能帮我吗?在此先感谢
答案 0 :(得分:0)
由于NSDictionary不是有序数据容器,因此您必须使用其他数据结构,或者必须更新API并在“数据”内部返回有序数组。
答案 1 :(得分:0)
另一种解决方案应该创建一个自定义解析器,该解析器会将您的数据转换为代表任何部分的模型数组。
模型
class SomethingObj {
var clave: String?
var orden: Int?
var nombre: String?
var video: String?
var imagen: String?
var series: Int?
var repeticiones: Int?
var descanso: Int?
var completado: Bool?
init() {
}
}
解析器
private func parseData(for structure: NSDictionary) -> [[SomethingObj]] {
var sectionsArray = [[SomethingObj]]()
guard let sectionsLoop = structure["data"] as? NSDictionary else { return sectionsArray }
var sectionIndex = 1
while let sectionObjsData = sectionsLoop["section\(sectionIndex)"] as? [NSDictionary] {
var sectionArray = [SomethingObj]()
for sectionObjData in sectionObjsData {
let obj = SomethingObj()
obj.clave = sectionObjData["clave"] as? String
obj.orden = sectionObjData["orden"] as? Int
obj.nombre = sectionObjData["nombre"] as? String
obj.video = sectionObjData["video"] as? String
obj.imagen = sectionObjData["imagen"] as? String
obj.series = sectionObjData["series"] as? Int
obj.repeticiones = sectionObjData["repeticiones"] as? Int
obj.descanso = sectionObjData["descanso"] as? Int
obj.completado = sectionObjData["completado"] as? Bool
sectionArray.append(obj)
}
sectionsArray.append(sectionArray)
sectionIndex = sectionIndex + 1
}
return sectionsArray
}
显示已解析的数据
使用任何您想使用的内容来显示已解析数据数组。
答案 2 :(得分:0)
首先,Section2
上方的JSON中有一个多余的右括号。
这是一个起点。
将data
的值解码为[String:[Item]]
,并将每个字典映射到包含名称(字典键)和Section
数组(字典值)的辅助结构Item
)。 sections
数组按name
struct Root : Decodable {
let code : Int
let message : String
let sections : [Section]
enum CodingKeys: String, CodingKey { case code, message, sections = "data"}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
code = try container.decode(Int.self, forKey: .code)
message = try container.decode(String.self, forKey: .message)
let data = try container.decode([String : [Item]].self, forKey: .sections)
sections = data.map({Section(name: $0.0, items: $0.1)}).sorted(by: {$0.name < $1.name})
}
}
struct Section {
let name : String
let items : [Item]
}
struct Item : Decodable {
let clave : String
let completado : Bool
let repeticiones : Int?
// ... other properties
}
解码Root
结构(data
是JSON数据)
let result = try JSONDecoder().decode(Root.self, from: data)
let sections = result.sections
在表格视图中,sections
是部分,items
是行