Swift 4 The data couldn’t be read because it isn’t in the correct format

前端 未结 2 1524
甜味超标
甜味超标 2021-01-12 19:23

I am developing a project to learn how to parse JSON. I am trying to parse JSON to a struct. I am trying to do it using the code that come

相关标签:
2条回答
  • 2021-01-12 19:59

    Try this

    let gitData = try decoder.decode(Root.self, from: data!)
    

    Traverse through your data

    for singleData in gitData.data where (singleData.name ?? "") == "Cafe" {
        print(singleData.image)
    }
    
    0 讨论(0)
  • 2021-01-12 20:19

    For me, the issue with "The data couldn’t be read because it isn’t in the correct format." was something to do with the specific XLSX file I was trying to use. I created a new XLSX file in Excel, and added it to my project and that worked! Then I just pasted the data I needed from the file that was giving me the error into the newly created XLSX file and it parsed!

        static func load() {
        
        guard let filePath = Bundle.main.path(forResource: "test", ofType: "xlsx", inDirectory: nil) else {
            fatalError("XLSX file not exist")
        }
        
        let file = XLSXFile(filepath: filePath)
        
        do {
                       
            let parseBooks = try? file?.parseWorkbooks()
            
            for eachBook in parseBooks! {
                
                for (name, path) in try file!.parseWorksheetPathsAndNames(workbook: eachBook) {
        
                    let worksheet = try file!.parseWorksheet(at: path)
                    let sharedStrings = try file!.parseSharedStrings()
                    print(sharedStrings)
        
                }
            }
        } catch {
            print("Error: \(error.localizedDescription)")
        }
      }
    
    0 讨论(0)
提交回复
热议问题