使用Codable解码这种JSON

时间:2019-10-14 21:25:06

标签: json swift codable

我有一个看起来像这样的json:

let jsonString = """
{
    "data": [
        {
            "name": "Apple",
            "price": 44,
            "available": true
        },
        {
            "name": "Pear",
            "price": 27,
            "available": false
        }
    ],
    "some_stuff": [],
    "some_other_stuff": []
}"""

我的目标是解析data数组的内容,特别是在不关心availibility属性的结构中。我不在乎some_stuff,而返回的some_other_stuff就是那个json。

  • 第一个问题:如果这些属性事件与我感兴趣的“数据”处于同一级别,是否可以忽略这些事件?如果是这样,我该如何表示并清空由于我只得到空数组而忽略的类型的数组? (为此,我选择将其表示为任意选择的Int类型的数组?)

我创建了一个结构:

struct Fruit: Codable {
    let name: String
    let price: Int
}

还有上面的一种超级结构:

struct WholeResponse: Codable {
    let data: [Fruit]
    let someStuff: [Int?]
    let someOtherStuff: [Int?]

    enum CodingKeys: String, CodingKey {
        case data
        case someStuff = "some_stuff"
        case someOtherStuff = "some_other_stuff"
    }

    struct Fruit: Codable {
        let name: String
        let price: Int
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let data = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .data)
        name = try data.decode(String.self, forKey: .name)
        price = try data.decode(Int.self, forKey: .price)
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        var data = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .data)
        try data.encode(name, forKey: .name)
        try data.encode(price, forKey: .price)
    }
}

但是在执行此操作后,我发现自己陷入了困境:

let jsonData = jsonString.data(using: .utf8)!
let jsonDecoder = JSONDecoder()

let fruits = try jsonDecoder.decode(WholeResponse.self, from: jsonData)

我认为我缺少有关数据表示的信息,可能会使事情复杂化,但是您能帮我解析此数据数组吗?

1 个答案:

答案 0 :(得分:2)

首先,删除您不需要的所有内容:

struct WholeResponse: Codable {
    let data: [Fruit]

    struct Fruit: Codable {
        let name: String
        let price: Int
    }
}

然后,像以前一样说

let response = try jsonDecoder.decode(WholeResponse.self, from: jsonData)
let fruits = response.data