解码有时可能会改变的数据

Sin*_*man 1 swift swift4 codable decodable

我有一个名为Info的结构,该结构根据接收到的数据进行解码。但是有时,数据中的值之一可以是double或double数组。我该如何设置我的结构?

这是我的结构:

struct Info: Decodable {

    let author: String
    let title: String
    let tags: [Tags]
    let price: [Double]

    enum Tags: String, Decodable {
        case nonfiction
        case biography
        case fiction
    }

}
Run Code Online (Sandbox Code Playgroud)

根据网址,我要么获得双倍价格

{
    "author" : "Mark A",
    "title" : "The Great Deman",
    "tags" : [
      "nonfiction",
      "biography"
    ],
    "price" : "242"

}
Run Code Online (Sandbox Code Playgroud)

或者我将其作为双打数组获得

{
    "author" : "Mark A",
    "title" : "The Great Deman",
    "tags" : [
      "nonfiction",
      "biography"
    ],
    "price" : [
    "242",
    "299",
    "335"
    ]

}
Run Code Online (Sandbox Code Playgroud)

我想设置我的结构,以便如果我收到一个double而不是double数组,则price应该解码为1 double的数组。

任何帮助将不胜感激。

谢谢

Leo*_*bus 5

您的JSON实际上是一个字符串或一个字符串数组。因此,您需要创建一个自定义解码器进行解码,然后将其转换为Double:

struct Info {
    let author, title: String
    let tags: [Tags]
    let price: [Double]
    enum Tags: String, Codable {
        case nonfiction, biography, fiction
    }
}
Run Code Online (Sandbox Code Playgroud)
extension Info: Codable {
    public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        author = try container.decode(String.self, forKey: .author)
        title  = try container.decode(String.self, forKey: .title)
        tags = try container.decode([Tags].self, forKey: .tags)
        do {
            price = try [Double(container.decode(String.self, forKey: .price)) ?? .zero]
        } catch {
            price = try container.decode([String].self, forKey: .price).compactMap(Double.init)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

游乐场测试

let infoData = Data("""
{
    "author" : "Mark A",
    "title" : "The Great Deman",
    "tags" : [
      "nonfiction",
      "biography"
    ],
    "price" : "242"

}
""".utf8)
do {
    let info = try JSONDecoder().decode(Info.self, from: infoData)
    print("price",info.price)  // "price [242.0]\n"
} catch {
    print(error)
}
Run Code Online (Sandbox Code Playgroud)
let infoData2 = Data("""
{
    "author" : "Mark A",
    "title" : "The Great Deman",
    "tags" : [
      "nonfiction",
      "biography"
    ],
    "price" : [
    "242",
    "299",
    "335"
    ]

}
""".utf8)

do {
    let info = try JSONDecoder().decode(Info.self, from: infoData2)
    print("price",info.price)  // "price [242.0, 299.0, 335.0]\n"
} catch {
    print(error)
}
Run Code Online (Sandbox Code Playgroud)