热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

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

如何解决《解码有时可能会改变的数据》经验,为你挑选了1个好方法。

我有一个名为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
    }

}

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

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

}

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

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

}

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

任何帮助将不胜感激。

谢谢



1> Leo Dabus..:

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

struct Info {
    let author, title: String
    let tags: [Tags]
    let price: [Double]
    enum Tags: String, Codable {
        case nonfiction, biography, fiction
    }
}

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)
        }
    }
}

游乐场测试

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)
}

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)
}


推荐阅读
author-avatar
风里火
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有