Исправить JSON (поскольку ваш JSON не полностью и содержит неправильные ,
ключи в конце дня рождения)
{
"Teams": [{
"Blue Team": {
"motto": "We're the best",
"players": [{
"name": "Bob",
"skill": "jumping really high",
"birthday": 1546326611
},
{
"name": "Julie",
"skill": "really strong",
"birthday": 1546413133
},
{
"name": "Kirsten",
"skill": "smarty pants",
"birthday": 1546499716
}
]
},
"Green Team": {
"motto": "We're the best",
"players": [{
"name": "Bob",
"skill": "jumping really high",
"birthday": 1546326611
},
{
"name": "Julie",
"skill": "really strong",
"birthday": 1546413133
},
{
"name": "Kirsten",
"skill": "smarty pants",
"birthday": 1546499716
}
]
}
}]
}
для 2 клавиш
struct Root: Codable {
let teams: [Team]
enum CodingKeys: String, CodingKey {
case teams = "Teams"
}
}
struct Team: Codable {
let blueTeam, greenTeam: BlueTeamClass
enum CodingKeys: String, CodingKey {
case blueTeam = "Blue Team"
case greenTeam = "Green Team"
}
}
struct BlueTeamClass: Codable {
let motto: String
let players: [Player]
}
struct Player: Codable {
let name, skill: String
let birthday: Int
}
для динамических клавиш
struct Root: Codable {
let teams: [[String:Item]]
enum CodingKeys: String, CodingKey {
case teams = "Teams"
}
}
struct Item: Codable {
let motto: String
let players: [Player]
}
struct Player: Codable {
let name, skill: String
let birthday: Int
}
Decode
do {
let res = try JSONDecoder().decode(Root.self,from:data)
print(res)
}
catch {
print(error)
}