ios - 快速 JSON 错误字典?我该怎么办
问题描述
所以我试图在我的 swift IOS 应用程序上获取正在播放的信息。
数据看起来像这样
{"data":[{"track":{"_id":"5dd87773c125b904113d7d39","partnerId":"DRN1","title":"Not Giving Up (Nu Disco Mix)","artist":"HP Vince & Dave Leatherman","album":"Disc 2: Traxsource \"Nu Disco & Indie Dance\"","website":"","imageurl":"http://covers.drn1.com.au/az_B1017221_Disc 2 Traxsource Nu Disco & Indie Dance_HP Vince & Dave Leatherman.jpg","datetime":"2019-11-23T00:04:03.245Z","__v":0}}]}
但是当我运行时出现此错误
nw_endpoint_flow_copy_multipath_subflow_counts Called on non-Multipath connection
error json typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "data", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "track", intValue: nil)], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))
我的代码
struct nowplayng: Decodable{
let data: [data]
}
struct data: Decodable{
let track: [trackinfo]
}
struct trackinfo: Decodable {
let title: String
let artist: String
let imageurl: String
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let jsonURLString = "https://api.drn1.com.au/station/playing"
guard let feedurl = URL(string: jsonURLString) else { return }
URLSession.shared.dataTask(with: feedurl) { (data,response,err)
in
guard let data = data else { return }
do{
let nowplaying = try JSONDecoder().decode(nowplayng.self, from: data)
print(nowplaying.data)
}catch let jsonErr{
print("error json ", jsonErr)
}
// let dataAsString = String(data:data, encoding: .utf8)
// print(dataAsString)
}.resume()
}
解决方案
替换(track
键是字典而不是数组)
let track: [Trackinfo]
和
let track:Trackinfo
推荐阅读
- javascript - 在字段更改时向服务发送更新的值,该字段已为先前的值执行一次
- bash - 如何在 bash 脚本中读取 Twitter 用户的个人资料
- sql - 如何从 $this->db->insert('history', $data) 获取 SQL 字符串正在构建并发送到 db codeigniter?
- regex - 用正则表达式替换结尾随机并添加字符
- java - java.lang.ClassCastException 在休眠中调用 session.getUniqueResult 或 session.get
- java - 我想添加 switch 语句来打开多活动(意图)
- python - 解析当前页面时如何正确调用另一个函数?
- c++ - 使用 cin 时如何使用 clock() 测量时间?
- ruby-on-rails - 如何发送带有图像背景的电子邮件而不将此图像附加为文件以供下载?
- mysql - 将 SSRS 报告中的小数四舍五入