这对我没有帮助: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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句