can't make this work.any ideas why it ain't working?

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

Replies

Why do you call JSONSerialization ? What do you expect ?

You do not show the whole code for

    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
}

It is probably a struct. Let's assume it is

struct DiskRecord { // Or is it VynilModel ?
    let …

Then, you should probably simply:

have declared a var

var disk : DiskRecord

change func as:

    func getData() -> DiskRecord? {
        
        guard let data = getJSONData() else { return nil }
        let jsonDecoder = JSONDecoder()
        do {
            let  disk = try jsonDecoder.decode(DiskRecord.self, from: data)
            return disk
        } catch {
            print("Decode error", error)
        }
        return nil
    }

Note that your struct needs to conform to Decodable in order to use JSONDecoder, like this:

struct Vinyl: Decodable { // assuming the same name as in your 2019 Stack Overflow post

And you should definitely switch if possible to Decodable and JSONDecoder as shown by @Claude31. Consider JSONSerialization unofficially deprecated for Swift.

Now as for this issue in the original code:

But it can't get through the line of the if let localData statement. I don't know why.

The “don’t know why” is due to using try? which silently eats any error thrown by the JSONSerialization.jsonObject() call. If you change it to regular try and enclose it in a do / catch block, then the error it catches will tell you exactly what the problem is.