如何在Swift数组中解析JSON数组?

时间:2018-10-13 18:22:20

标签: arrays json swift codable decodable

我正在尝试解析如下的JSON:

"price": [
            [
                1539283140000,
                6288.07
            ],
            [
                1539283440000,
                6285.82
            ],
            [
                1539283740000,
                6285.81
            ],
            [
                1539284041000,
                6280.37
            ],
            [
                1539284340000,
                6280.19
            ]

请帮助我处理这个问题。并且可以将时间戳记值解码为日期。

1 个答案:

答案 0 :(得分:0)

更正json

{
    "price": [
            [
                1539283140000,
                6288.07
            ],
            [
                1539283440000,
                6285.82
            ],
            [
                1539283740000,
                6285.81
            ],
            [
                1539284041000,
                6280.37
            ],
            [
                1539284340000,
                6280.19
            ]
         ]
}

struct Root: Codable {
    let price: [[Double]]
}

let res = try? JSONDecodable().decode(Root.self,from:jsonData)