【问题标题】:swift JSON error dictionary? what do I do快速 JSON 错误字典?我该怎么办
【发布时间】:2020-03-19 01:46:56
【问题描述】:

所以我正在尝试在我的 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()



    }

【问题讨论】:

  • 结构体名称以大写字母开头

标签: ios json swift dictionary


【解决方案1】:

替换(track 键是字典而不是数组)

let track: [Trackinfo]

let track:Trackinfo

【讨论】:

  • 谢谢,我会将此标记为正确,唯一的另一个问题是我可以打印出现在正在打印数据的曲目标题 [DRN1.Data(track: DRN1.Trackinfo(title:" Charly\'s Ballad (Original Mix)", 艺术家: "Castle Queenside", imageurl: "covers.drn1.com.au/az_B1017197_Disc 1 Traxsource Nu Disco & Indie Dance_Castle Queenside.jpg"))]
猜你喜欢
  • 2021-08-06
  • 2015-06-22
  • 1970-01-01
  • 2012-04-14
  • 1970-01-01
  • 2013-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多