这对我没有帮助:Swift 4 解析 1+n 数量的 json 数字键
情况有所不同。
我有这个 JSON(稍微简化了一点):
{
"1": {
"name": "Tea room",
"description": "A room for tea"
},
"2": {
"name": "Playground",
"description": "Here you can play all day long"
},
"3": {
"name": "Kitchen",
"description": "Hungry persons can go here"
}
}
我的问题是如何解码这个。我正在使用 Swift 版本 5。并且我正在尝试使用 JSONDecoder().decode();
键“1”、“2”和“3”实际上是 ID,由 API 生成。我不知道我会收到多少房间。我不知道他们将拥有什么密钥,但我需要知道密钥(ID)才能进一步进行操作。
我正在做这样的事情:
struct Rooms: Decodable {
let id: Int; //THIS ONE IS MY PROBLEM
let rooms: [Room]?;
}
struct Room: Decodable {
let name: String?;
let description: String?;
private enum CodingKeys: String, CodingKey {
case name, description;
}
}
//Here trying to decode the JSON I got back.
JSONDecoder().decode([Group].self, from: jsonResult);
这让我很头疼:)
谁能帮助我朝正确的方向前进吗?
您可以将其解码为字典:[String: Room]
:
JSONDecoder().decode([String: Room].self, from: jsonResult);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)