JSON 解码 Swift 中的双重嵌套数组

JSON decoding double nested array in Swift

目前我正在尝试使用嵌套数组解码 JSON。嵌套数组可以在其中包含对象的一些随机数。我尝试对其进行解码,但结果是 return 错误

CodingKeys(stringValue: "itenaries", intValue: nil),
debugDescription : "Expected to decode Array<Any> but found a dictionary 

样本JSON数据

{
   "itenaries": {
      "days":
      [
         [
            {
               "itenary_id":0,
               "itenary_location_name":"Batu Caves Temple"
            }
     
         ],
         [
            {
               "itenary_id":0,
               "itenary_location_name":"KL Tower "
            },
            {
               "itenary_id":1,
               "itenary_location_name":"KL Forest Eco Park"
            }
         ]
      ]
   }
}

我的结构

struct Itenaries : Codable {
    let itenaries : [[Days]]
}

struct Days : Codable {
    let itenary_id : Int
    let itenary_location_name : String
}

解码实现

let decoder = JSONDecoder()
let itenary = try decoder.decode(Itenaries.self, from: fileData)
print(itenary.itenaries[0][0].itenary_id)

你在哪里解码 days 密钥?那就是问题所在。你需要一个中间结构

struct Root : Decodable {
    let itenaries : Itenary
}

struct Itenary : Decodable {
    let days : [[Days]]
}

...

let result = try decoder.decode(Root.self, from: fileData)
print(result.iternaries.days[0][0].itenary_id)

我可能会做类似的事情

struct Name:Codable {
    var itenaries:itenaries
}

struct itenaries:Codable {
    var days = [[Days]]
}
struct Days : Codable {
    let itenary_id : Int
    let itenary_location_name : String
}

所以基本上根据你的 Json 文件的结构 根结构 -> itenaries -> [[days]]

希望你明白:)

[已编辑] 你可以试试这些。 我使用这种方法得到了正确的结果

Result

struct MainResponse : Codable {
    let itenaries : Itenaries
}

struct Itenaries : Codable {
    let days : [[Days]]
}

struct Days : Codable {
    let itenary_id : Int
    let itenary_location_name : String
}

if let path = Bundle.main.path(forResource: "nested_array", ofType: "json") {
    do {
        let responseData = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
        let decoder = JSONDecoder()
        let mainResponse = try decoder.decode(MainResponse.self, from: responseData)
        print(mainResponse.itenaries.days[0][0].itenary_id)
        print(mainResponse.itenaries.days[0][0].itenary_location_name)
        print(mainResponse.itenaries.days[1][0].itenary_id)
        print(mainResponse.itenaries.days[1][0].itenary_location_name)
        print(mainResponse.itenaries.days[1][1].itenary_id)
        print(mainResponse.itenaries.days[1][1].itenary_location_name)

        // output
        // 0
        // Batu Caves Temple
        // 0
        // KL Tower 
        // 1
        // KL Forest Eco Park
    } catch let error {
        print(error.localizedDescription)
    }
}

您的模型不正确,请替换为:

struct ItenariesResponse: Codable {
    let itenaries: Itenaries
}

struct Itenaries: Codable {
    let days: [[Day]]
}

struct Day: Codable {
    let itenaryID: Int
    let itenaryLocationName: String

    enum CodingKeys: String, CodingKey {
        case itenaryID = "itenary_id"
        case itenaryLocationName = "itenary_location_name"
    }
}

然后像这样替换你解码的类型:

let itenary = try decoder.decode(ItenariesResponse.self, from: fileData)