我从 Reddit API 获取 JSON 格式的 Reddit 帖子评论(针对特定 subreddit 的一篇帖子),然后通过 Structs 解析 JSON。当我尝试输出解码的注释时,出现错误:
解码 Json 注释时出错 - typeMismatch(Swift.Dictionary
,
Swift.DecodingError.Context(codingPath: [], debugDescription: “预计解码 Dictionary但发现一个数组 相反。”,underlyingError: nil))
也许我在结构中遗漏了某些内容,或者存储库
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 结构
您提到您有以下错误:
typeMismatch(Swift.Dictionary<String, Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil))
让我们来解构一下:
您可以从中读到以下两点有用的内容:
首先,
debugDescription
:预期解码字典这意味着您正在尝试解码字典,但 JSON 包含数组。请注意,您标记为可编码的大多数普通类型都会编码到字典中。
其次,
codingPath
,在您的情况下是一个空数组([]
),这意味着这个问题就在您尝试解码的根类型上。
现在让我们看一下您发布的 Postman 回复。在第 1 行,您可以看到最外层的容器(第 1 行)是一个数组。
但是当您解码时,您正在解码
CommentListing
,它使用带密钥的容器(字典)。
因此要解决此问题,您必须解码
CommentListing
数组。
let postsResponse = try JSONDecoder().decode([CommentListing].self, from: jsonData!)
尝试将您的响应解码为
CommentListing
数组,例如:
do {
let postsResponse = try JSONDecoder().decode([CommentListing].self, from: jsonData!)
print(postsResponse)
completion(postsResponse)
} catch {
print("Error decoding Json comments - \(error)")
completion(nil)
}