我想要以下 JSON 结构(假示例):
{
"Admin" :
{
"name" : "John",
"age" : "42"
},
"Sales" :
{
"name" : "John",
"age" : "42"
},
"CEO" :
{
"name" : "Peter",
"age" : "52",
"salary" : "100000"
},
"Janitor" :
{
"name" : "Matthew",
"age" : "22"
}
}
如您所见,结构如果确定,但结构的名称是动态的。
如何将其转换为 Swift Codable 结构?当前尝试:
struct Positions: Codable
{
var posDicts: [String: Position] = [:]
}
struct Position: Codable
{
let name: String
let age: Int
let salary: Int?
}
但是,这将给出以下结果:
"posDicts" : {
"Admin" :
{
"name" : "John",
"age" : "42"
},
"Sales" :
{
"name" : "John",
"age" : "42"
},
"CEO" :
{
"name" : "Peter",
"age" : "52",
"salary" : "100000"
},
"Janitor" :
{
"name" : "Matthew",
"age" : "22"
}
}
我不想要 JSON 中的“posDicts”。什么是最好/最简单的解决方案?