Hi!
I am trying to parse a local JSON file but can't get it to work. The command print(title) is not being printed.
Here is my code:
func getJSONData() -> Data? {
if let url = Bundle.main.url(forResource: "teste", withExtension: "json"),let data = try? Data(contentsOf: url) {
return data
} else {
return nil
}
}
init() {
getData()
}
func getData() {
guard let data = getJSONData() else { return }
if let localData = try? JSONSerialization.jsonObject(with: data,options: []),let dictionary = localData as? [String:Any] {
let id = dictionary["id"] as? Int ?? 1
let title = dictionary["title"] as? String ?? ""
let artist = dictionary["artist"] as? String ?? ""
let isOut = dictionary["isOut"] as? String ?? ""
let label = dictionary["label"] as? String ?? ""
let vinylAudoID = dictionary["vinylAudoID"] as? String ?? ""
let vinylCountry = dictionary["vinylCountry"] as? String ?? ""
let vinylFormat = dictionary["vinylFormat"] as? String ?? ""
let vinylID = dictionary["vinylID"] as? String ?? ""
let vinylLocation = dictionary["vinylLocation"] as? String ?? ""
let year = dictionary["year"] as? String ?? ""
print(title)
}
}
what am I missing?
thank you