gpt4 book ai didi

json - Swift 4 可解码,直到解码时才知道 key

转载 作者:IT老高 更新时间:2023-10-28 12:45:47 27 4
gpt4 key购买 nike

Swift 4 Decodable 协议(protocol)如何处理包含直到运行时才知道名称的键的字典?例如:

  [
{
"categoryName": "Trending",
"Trending": [
{
"category": "Trending",
"trailerPrice": "",
"isFavourit": null,
"isWatchlist": null
}
]
},
{
"categoryName": "Comedy",
"Comedy": [
{
"category": "Comedy",
"trailerPrice": "",
"isFavourit": null,
"isWatchlist": null
}
]
}
]

这里有一个字典数组;第一个有键 categoryNameTrending,而第二个有键 categoryNameComedycategoryName 键的值告诉我第二个键的名称。我如何使用 Decodable 来表达?

最佳答案

关键在于您如何定义 CodingKeys 属性。虽然它最常见的是 enum,但它可以是任何符合 CodingKey 协议(protocol)的东西。要制作动态键,您可以调用静态函数:

struct Category: Decodable {
struct Detail: Decodable {
var category: String
var trailerPrice: String
var isFavorite: Bool?
var isWatchlist: Bool?
}

var name: String
var detail: Detail

private struct CodingKeys: CodingKey {
var intValue: Int?
var stringValue: String

init?(intValue: Int) { self.intValue = intValue; self.stringValue = "\(intValue)" }
init?(stringValue: String) { self.stringValue = stringValue }

static let name = CodingKeys.make(key: "categoryName")
static func make(key: String) -> CodingKeys {
return CodingKeys(stringValue: key)!
}
}

init(from coder: Decoder) throws {
let container = try coder.container(keyedBy: CodingKeys.self)
self.name = try container.decode(String.self, forKey: .name)
self.detail = try container.decode([Detail].self, forKey: .make(key: name)).first!
}
}

用法:

let jsonData = """
[
{
"categoryName": "Trending",
"Trending": [
{
"category": "Trending",
"trailerPrice": "",
"isFavourite": null,
"isWatchlist": null
}
]
},
{
"categoryName": "Comedy",
"Comedy": [
{
"category": "Comedy",
"trailerPrice": "",
"isFavourite": null,
"isWatchlist": null
}
]
}
]
""".data(using: .utf8)!

let categories = try! JSONDecoder().decode([Category].self, from: jsonData)

(我将 JSON 中的 isFavourit 更改为 isFavourite,因为我认为这是拼写错误。如果不是这种情况,修改代码很容易)

关于json - Swift 4 可解码,直到解码时才知道 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45598461/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com