Posts

Post not yet marked as solved
2 Replies
211 Views
Hi ! I have this code bellow in my project and I am trying to parse a local JSON file.But it can't get through the line of the if let localData statement. I don't know why.     let id: Int     let title: String     let artist: String     let isOut: String     let label: String     let vinylAudoID: String     let vinylCountry: String     let vinylFormat: String     let vinylID: String     let vinylLocation: String     let year: String } class CodableViewModel: ObservableObject {     @Published var vinyl: VinylModel? = 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)         }              }          func getJSONData() -> Data? {         if let url = Bundle.main.url(forResource: "*****", withExtension: "json"),let data = try? Data(contentsOf: url) {             return data         } else {             print("DEU RUIM")             return nil         }     }      } any ideas why this is happening? If I put those if lets in separated lines it enters the if let localData but it does not enter the if let dictionary = localData as? [String:Any] thank u
Posted Last updated
.
Post not yet marked as solved
0 Replies
362 Views
Hi! I have a json file with over 3.000 records but some of them are empty.How do I handle empty fields on my JSON file? I am trying to parse it but it is not working. Nothing gets printed because it won't pass line if let jsonData = try? decoder().decode ....         if let url = Bundle.main.url(forResource: "vinyls", withExtension: "json"),let data = try? Data(contentsOf: url) {             let decoder = JSONDecoder()             if let jsonData = try? decoder.decode(jsonVinylData.self, from: data) {                 print(jsonData.vinyls)             }         }      } thank u
Posted Last updated
.