[英]Decodable nested item in JSON with Swift
I am trying to decode some JSON I am getting from the reddit api.我正在尝试解码我从 reddit api 获得的一些 JSON。 I want to create an array of objects from the data.
我想从数据创建一个对象数组。 The issue is I want to get the data but it is nested two levels deep.
问题是我想获取数据,但它嵌套了两层深。
This is the model这是模型
struct Moderator: Codable {
var name: String
}
Here is the part in my network manager where it is trying to get the correct data.这是我的网络管理器中尝试获取正确数据的部分。 It keeps hitting the catch block:
它不断击中 catch 块:
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let moderators = try decoder.decode([Moderator].self, from: data)
completed(moderators, nil)
} catch {
completed(nil, "Invalid data")
}
I am getting the json from here:我从这里获取 json:
https://www.reddit.com/r/videos/about/moderators.json
A sample from it:来自它的一个样本:
kind "UserList"
data
children
0
name "name3465"
author_flair_text null
date 1297779059
rel_id "rb_c32nl"
id "t2_4d9s0"
author_flair_css_class null
1
name "name2279"
author_flair_text null
date 1300169101
rel_id "rb_d151z"
id "t2_1f8e1"
author_flair_css_class null
As an example ModeratorResponse
and Moderator
would be the struct you wish to use in the app, and RawModerator
is a private struct that hides the parsing that ignores the nested structures in between the data you are interested.例如,
ModeratorResponse
和Moderator
将是您希望在应用程序中使用的结构,而RawModerator
是一个私有结构,它隐藏了忽略您感兴趣的数据之间的嵌套结构的解析。 Take a look at this example you can try on online.swiftplayground.run :看看这个例子,你可以试试online.swiftplayground.run :
import Foundation
private struct RawModerator: Decodable {
struct Data: Decodable {
var children: [Moderator]
}
var kind: String
var data: Data
}
struct ModeratorResponse: Decodable {
var moderatorList: [Moderator]
init(from decoder: Decoder) throws {
let rawResponse = try RawModerator(from: decoder)
moderatorList = rawResponse.data.children
}
}
struct Moderator: Decodable {
var name: String?
var authorFlairTxt: String?
var permissions: [String]?
var date: Double?
var rel_id: String
var id: String
var authorFlairCssClass: String?
enum CodingKeys: String, CodingKey {
case name = "name"
case authorFlairTxt = "author_flair_text"
case permissions = "mod_permissions"
case date = "date"
case rel_id = "rel_id"
case id = "id"
case authorFlairCssClass = "author_flair_css_class"
}
}
let json = """
{
"kind": "UserList",
"data": {
"children": [
{
"name": "doug3465",
"author_flair_text": null,
"mod_permissions": ["all"],
"date": 1297779059.0,
"rel_id": "rb_c32nl",
"id": "t2_4d9s0",
"author_flair_css_class": null
},
{
"name": "joka86",
"author_flair_text": null,
"mod_permissions": ["all"],
"date": 1571872041.0,
"rel_id": "rb_1k9iezh",
"id": "t2_36u6d",
"author_flair_css_class": null
}
]
}
}
""".data(using: .utf8)!
if let results = try? JSONDecoder().decode(ModeratorResponse.self, from: json) {
for moderator in results.moderatorList {
print("ID: \(moderator.id)")
print("Name: \(moderator.name)")
}
}
Which outputs:哪些输出:
ID: t2_4d9s0 Name: Optional("doug3465") ID: t2_36u6d Name: Optional("joka86")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.