将嵌套的JSON解码为采用Decodable的结构

时间:2018-07-25 06:58:11

标签: json swift decodable

我有以下要解码的JSON:

{
    "id":5,
    "items_received":[
        {
            "id":1,
            "name":"Doug"
        },
        {
            "id":2,
            "name":"smith"
        }
    ]
}

我使用以下结构:

struct Delivery: Equatable, Decodable {
    let id: Int
    let items: [Item]

    private enum CodingKeys: String, CodingKey {
        case id
        case items = "items_received"
    }
}

struct Item: Equatable, Decodable {
    let id: Int
    let name: String
}

并致电:

{ try self.jsonDecoder.decode(Delivery.self, from: value) }

并获得:

  

失败:keyNotFound(CodingKeys(stringValue:“ items_received”,   intValue:无)

0 个答案:

没有答案