【发布时间】:2021-03-11 02:31:33
【问题描述】:
我从 Reddit API(针对特定 subreddit 的一篇帖子)以 JSON 格式获取 reddit 帖子 cmets,然后通过 Structs 解析 JSON。当我尝试输出解码后的 cmets 时出现错误:
错误解码 Json cmets - typeMismatch(Swift.Dictionary
, Swift.DecodingError.Context(codingPath: [], debugDescription: “应解码 Dictionary 但找到一个数组 而是。”,基础错误:无))
也许我在结构体中遗漏了某些内容,或者在 Repository getComments 方法中遗漏了不匹配的类型。请指教。
enum RequestURL {
case top(sub: String, limit: Int)
case postAt(sub: String, id: String)
var url: String {
switch self {
case .top(let sub, let limit):
return "https://www.reddit.com/r/\(sub)/top.json?limit=\(limit)"
case .postAt(let sub, let id):
return "https://www.reddit.com/r/\(sub)/comments/\(id).json"
}
}
}
class HTTPRequester {
init() {}
func getData (url: RequestURL, completion: @escaping(Data?) -> Void) {
guard let url = URL(string: url.url) else {
print("Error: Request URL is nil!")
completion(nil)
return
}
URLSession.shared.dataTask(with: url) {data,_,error in
guard let jsonData = data else {
print(error ?? "Error")
completion(nil)
return
}
completion(jsonData)
}.resume()
}
}
class Service {
init() {}
func decodeJSONComments(url: RequestURL, completion: (@escaping (_ data: CommentListing?) -> Void)) {
HTTPRequester().getData(url: url) { jsonData in
do {
let postsResponse = try JSONDecoder().decode(CommentListing.self, from: jsonData!)
print(postsResponse)
completion(postsResponse)
} catch {
print("Error decoding Json comments - \(error)")
completion(nil)
}
}
}
}
class Repository {
init() {}
func getComments(sub: String, postId: String, completion: (@escaping ([RedditComment]) -> Void)) {
Service().decodeJSONComments(url: RequestURL.postAt(sub: sub, id: postId)) { (comments: CommentListing?) in
var commentsList = [CommentData]()
commentsList = (comments?.data.children) ?? []
let mappedComs = commentsList.map { (comment) -> RedditComment in
return RedditComment(
id: comment.data.id,
author: comment.data.author,
score: comment.data.score,
body: comment.data.body)
}
completion(mappedComs)
}
}
}
class UseCase {
func createComments(sub: String, postId: String, completion: (@escaping (_ data: [RedditComment]) -> Void)) {
Repository().getComments(sub: sub, postId: postId) { (comments: [RedditComment]) in
completion(comments)
}
}
}
UseCase().createComments(sub: "ios", postId: "4s4adt") { comments in
print(comments)
}
JSON 结构
【问题讨论】: