This one does not help me: Swift 4 parsing json numeric keys with 1+n amount
The case is different.
I have this JSON (simplified a bit):
{
"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"
}
}
My problem is how to decode this. I am using Swift version 5. And I am trying to use JSONDecoder().decode();
The keys "1", "2" and "3" are actually Ids and are generated by the API. I don't know how many rooms I will receive. And I don't know what key they will have, but I need to know the key (the Id) to be able to go further.
I am working on something like this:
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);
This is giving me a headache :)
Can anyone please help me in the right direction?