RussellHarrower
RussellHarrower

Reputation: 6790

swift JSON error dictionary? what do I do

So I am trying to get nowplaying info working on my swift IOS app.

the data looks like this

{"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}}]}

but when I run I get this error

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))

mycode

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()



    }

Upvotes: 0

Views: 141

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

Replace ( track key is a dictionary not an array )

let track: [Trackinfo]

with

let track:Trackinfo

Upvotes: 1

Related Questions