gpt4 book ai didi

json - Swift:可编码复杂的 JSON

转载 作者:行者123 更新时间:2023-11-28 11:37:29 25 4
gpt4 key购买 nike

当我点击 wiki API 时,我收到 JSON 响应,如下所示。我发现解码它很复杂。

{
"continue": {
"picontinue": 452160,
"continue": "||pageterms"
},
"query": {
"pages": [
{
"pageid": 164053,
"ns": 0,
"title": "Abdullah II of Jordan",
"index": 2,
"terms": {
"description": [
"King of the Hashemite Kingdom of Jordan"
]
}
},
{
"pageid": 348097,
"ns": 0,
"title": "Abdullah Ahmad Badawi",
"index": 9,
"terms": {
"description": [
"Malaysian politician"
]
}
},
{
"pageid": 385658,
"ns": 0,
"title": "Abdelaziz Bouteflika",
"index": 8,
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5b/Abdelaziz_Bouteflika_casts_his_ballot_in_May_10th%27s_2012_legislative_election_%28cropped%29.jpg/37px-Abdelaziz_Bouteflika_casts_his_ballot_in_May_10th%27s_2012_legislative_election_%28cropped%29.jpg",
"width": 37,
"height": 50
},
"terms": {
"description": [
"President of Algeria"
]
}
},
{
"pageid": 452160,
"ns": 0,
"title": "Abdul Qadeer Khan",
"index": 7,
"terms": {
"description": [
"Pakistani nuclear scientist"
]
}
},
{
"pageid": 2028438,
"ns": 0,
"title": "Abdelbaset al-Megrahi",
"index": 6,
"terms": {
"description": [
"Libyan mass murderer"
]
}
},
{
"pageid": 4709709,
"ns": 0,
"title": "Abdul",
"index": 1,
"terms": {
"description": [
"family name"
]
}
},
{
"pageid": 18950786,
"ns": 0,
"title": "Abdul Hamid II",
"index": 5,
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/0/07/Abdul_Hamid_2.jpg/35px-Abdul_Hamid_2.jpg",
"width": 35,
"height": 50
},
"terms": {
"description": [
"34th sultan of the Ottoman Empire"
]
}
},
{
"pageid": 19186951,
"ns": 0,
"title": "Abdullah of Saudi Arabia",
"index": 4,
"terms": {
"description": [
"former King of Saudi Arabia"
]
}
},
{
"pageid": 25955055,
"ns": 0,
"title": "Abdullah of Pahang",
"index": 10,
"terms": {
"description": [
"Sultan of Pahang"
]
}
},
{
"pageid": 36703624,
"ns": 0,
"title": "Abdel Fattah el-Sisi",
"index": 3,
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3e/Abdel_Fattah_el-Sisi_in_2017.jpg/39px-Abdel_Fattah_el-Sisi_in_2017.jpg",
"width": 39,
"height": 50
},
"terms": {
"description": [
"Current President of Egypt"
]
}
}
]
}
}

我只对来自上述 JSON 的 WikiPage 数据数组感兴趣,其中结构应如下所示。

struct Wikipage: Decodable {
var pageid: Int?
var thumbnail: String?
var title: String?
var description: String?
enum CodingKeys: String, CodingKey {
case query
case pages
case terms
case description
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let query = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .query)
print(query)
}
}

由于出现数据不匹配错误,因此我无法在此处进一步深入了解查询。我可以在不使用额外的类/变量来保存不需要的数据的情况下进行解码吗?在这种情况下我们如何解码?

        let wiki = try decoder.decode([Wikipage].self, from: data!)

最佳答案

您要求的语法是不可能的。

let wiki = try decoder.decode([Wikipage].self, from: data!)

这对维基页面数组进行解码。 stdlib 中已经有一个解码器,但它不能满足您的要求。你无法更换它。您需要自己的类型来包装它。该类型除了包装结果之外不必执行任何操作,但它必须存在。以下是构建它的方法。

首先,WikiPage 应该只是 Wikipage 部分。它不应该尝试了解有关其容器的任何信息。所以它看起来像这样:

struct Wikipage {
let pageid: Int
let thumbnail: URL?
let title: String
let description: String
}

extension Wikipage: Decodable {
private enum CodingKeys: String, CodingKey {
case pageid
case title
case thumbnail
case terms
}

private struct Thumbnail: Decodable { let source: String }

private struct Terms: Decodable { let description: [String] }

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.pageid = try container.decode(Int.self, forKey: .pageid)
self.title = try container.decode(String.self, forKey: .title)

let source = try container.decodeIfPresent(Thumbnail.self,
forKey: .thumbnail)?.source
self.thumbnail = source.flatMap(URL.init(string:))

self.description = try container.decode(Terms.self,
forKey: .terms).description.first ?? ""
}
}

我删除了不需要可选的内容的可选性,并将 URL 之类的内容提升为正确的类型。请注意私有(private)帮助器结构(缩略图和术语)。这些负责嵌套。

同样的方法也适用于顶层。无需解码任何您不想要的内容,但您确实需要一种类型来保存它。

struct WikipageResult: Decodable {
let pages: [Wikipage]

private enum CodingKeys: String, CodingKey {
case query
}
private struct Query: Decodable {
let pages: [Wikipage]
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.pages = try container.decode(Query.self, forKey: .query).pages
}
}

最后使用它:

let pages = try JSONDecoder().decode(WikipageResult.self, from: json).pages

关于json - Swift:可编码复杂的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54669627/

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