gpt4 book ai didi

json - Swift 4 可解码 : struct from nested array

转载 作者:搜寻专家 更新时间:2023-10-31 08:03:50 26 4
gpt4 key购买 nike

鉴于以下 JSON 文档,我想创建一个具有四个属性的 struct:filmCount (Int)、year (Int) 、category(字符串)和actor(Actor 数组)。

{    
"filmCount": 5,
"year": 2018,
"category": "Other",
"actors":{
"nodes":[
{
"actor":{
"id":0,
"name":"Daniel Craig"
}
},
{
"actor":{
"id":1,
"name":"Naomie Harris"
}
},
{
"actor":{
"id":2,
"name":"Rowan Atkinson"
}
}
]
}
}

PlacerholderData 是一个存储三个主要属性和应从 actors 内的嵌套 nodes 容器中检索的 actors 列表的结构来自 JSON 对象的属性。

占位符数据:

struct PlaceholderData: Codable {
let filmCount: Int
let year: Int
let category: String
let actors: [Actor]
}

Actor.swift:

struct Actor: Codable {
let id: Int
let name: String
}

我试图通过提供我自己的 init 来手动初始化解码器容器中的值来做到这一点。我怎样才能解决这个问题而不必有一个存储 nodes 对象的中间结构?

最佳答案

您可以使用 nestedContainer(keyedBy:)nestedUnkeyedContainer(forKey:) 像这样解码嵌套数组和字典,将其转换为您想要的结构。你在 init(decoder: ) 中的解码可能看起来像这样,

用于解码的 Actor 扩展,

extension Actor: Decodable {

enum CodingKeys: CodingKey { case id, name }

enum ActorKey: CodingKey { case actor }

init(from decoder: Decoder) throws {
let rootKeys = try decoder.container(keyedBy: ActorKey.self)
let actorContainer = try rootKeys.nestedContainer(keyedBy: CodingKeys.self,
forKey: .actor)
try id = actorContainer.decode(Int.self,
forKey: .id)
try name = actorContainer.decode(String.self,
forKey: .name)
}
}

用于解码的 PlaceholderData 扩展,

extension PlaceholderData: Decodable {

enum CodingKeys: CodingKey { case filmCount, year, category, actors }

enum NodeKeys: CodingKey { case nodes }

init(from decoder: Decoder) throws {
let rootContainer = try decoder.container(keyedBy: CodingKeys.self)
try filmCount = rootContainer.decode(Int.self,
forKey: .filmCount)
try year = rootContainer.decode(Int.self,
forKey: .year)
try category = rootContainer.decode(String.self,
forKey: .category)
let actorsNode = try rootContainer.nestedContainer(keyedBy: NodeKeys.self,
forKey: .actors)
var nodes = try actorsNode.nestedUnkeyedContainer(forKey: .nodes)
var allActors: [Actor] = []

while !nodes.isAtEnd {
let actor = try nodes.decode(Actor.self)
allActors += [actor]
}
actors = allActors
}
}

然后,你可以这样解码,

let decoder = JSONDecoder()
do {
let placeholder = try decoder.decode(PlaceholderData.self, from: jsonData)
print(placeholder)
} catch {
print(error)
}

这里的基本思想是使用nestedContainer(keyedBy:)解码字典容器,使用nestedUnkeyedContainer(forKey:)解码数组容器

关于json - Swift 4 可解码 : struct from nested array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49437128/

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