我已经看到了几个类似的问题,但是我有不同的JSON
所以我的JSON如下所示
var json = """
{
"Array1": [
{
"FinancialYear": "17-18"
}],
"Array2": [
{
"FinancialYear": "17-18"
}]
}
"""
问题是 Array1 和 Array2 键,这些键似乎是动态的,并且位于ROOT上,可能更像Array3,Array4等
我想使用Codable,但是由于根目录(Array1,Array2)上的动态键,我无法摆脱它。
这是我尝试过但未工作的Struct
struct CodableJSON: Codable {
var response:[String:[ArrayInside]]
enum CodingKeys: String, CodingKey {
case response = "What should I write here ?" // What should be here ?
}
}
答案 0 :(得分:4)
在这种情况下,仅声明ArrayInside
结构
struct ArrayInside: Decodable {
...
}
并将根对象解码为字典
let result = try JSONDecoder().decode([String:[ArrayInside]].self, from: data)