如何解决在 Swift 中使用随机密钥解析 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);
这让我头疼:)
谁能帮我找到正确的方向?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。