我在使用swift 4解码和解析这个JSON文件数据时遇到了问题 . 我不知道如何在代码的底部构造我的结构,以便它们可以遍历我的数组字典数据 . 即 . 数据[0],数据[1],数据[2]等 . 这是我遇到的错误:
序列化JSON keyNotFound时出错(whatsong.RecentTvList . (_882383248D6F034EC588461F3FFE705E中的CodingKeys).title,Swift.DecodingError.Context(codingPath:[],debugDescription:"No value associated with key title (" title \ ").",underlyingError:nil))
{
data: [
{
_id: 100284,
poster: "urlstring",
title: "Happy!"
},
{
_id: 100284,
poster: "urlstring",
title: "Happy!"
}
]
}
let jsonUrlString = "HIDDEN"
guard let url = URL(string: jsonUrlString) else
{return}
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else {return}
do {
let show = try
JSONDecoder().decode(RecentTvList.self, from: data)
print(show)
} catch let jsonErr {
print("Error serializing JSON", jsonErr)
}
}.resume()
struct RecentTvListData: Decodable {
var data: [RecentTvList]
}
struct RecentTvList: Decodable {
var title: String?
var poster: String?
}
3 回答
我在你的回答中看到的问题是
data: []
实际上你得到这个字典的回复
在数组中
并且您正在直接尝试传递响应
Data
,其中包含RecentTvList
中的字典数组,其中只接受Dictionary
这就是为什么它无法识别键title
为错误建议EDIT
尝试更换
同
希望有助于解决您的问题
查看您提供的JSON . 响应中没有变量名
poster_url
. 该值应该只是poster
. 另外,您省略了-id
变量 .我认为你的JSON结构应该是这样的: