I'm having problems transferring my parsing code over to Swift4/Xcode9. I'm parsing it through my model correctly and I know the problem is because I'm currently parsing through the root JSON object, however my REST API is structured JSON root -> data array -> further objects. I know it's probably simple but I've been struggling for ages. I have attached an image of the layout of my REST API, and just need to know how to step into objects to retrieve values.
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(TvHeaderModel.self, from: data)
print(show.title)
} catch let jsonErr {
print("Error serializing JSON", jsonErr)
}
}.resume()
struct TvHeaderModel: Decodable {
var id: Int?
var title: String?
var year: Int?
var songCount: Int?
var poster: String?
init(json: [String: Any]) {
id = json["_id"] as? Int ?? -1
title = json["title"] as? String ?? ""
year = json["year"] as? Int ?? -1
songCount = json["song_count"] as? Int ?? -1
poster = json["poster_urL"] as? String ?? ""
}
}
...from: data["data"]), rather than...from: data)as the root object does not match yourTvHeaderModelstructure at all, but the value of the"data"shows some similarities...