简体   繁体   中英

Unable to decode with Swift decodable

I can not figure out why this doesn't work. On the internet I found a lot of solutions with local json(not api) or with api without apiKey which in my case must be in the header(tested in Postman).

struct Categories: Decodable {
        let name: String
        let id: Int
    }
    
    class ViewController: UIViewController {
        
        
        let api_key = "..."
        
        let urlString = "..."

        override func viewDidLoad() {
            super.viewDidLoad()
            
            let url = URL(string: urlString)
            
            var request = URLRequest(url: url!)
            
            request.addValue(api_key, forHTTPHeaderField: "user-key")
            request.httpMethod = "GET"
            
            URLSession.shared.dataTask(with: request) { data, response, error in
                if let data = data {
                    if let category = try? JSONDecoder().decode(Categories.self, from: data) {
                            print(category.name)
                        return
                    }
                }
                
                print("Fetch failed: \(error?.localizedDescription ?? "Unknown error")")
            }.resume()
        }
    }

I removed the urlString and api_key from the code above, but I am sure they are ok because I tested them in Postman
Problem - always getting printed Fetch failed: Unknown error
Here is how my json looks like:

{
    "categories": [
        {
            "categories": {
                "id": 1,
                "name": "A"
            }
        },
        {
            "categories": {
                "id": 2,
                "name": "B"
            }
        }....

Instead of just categories struct... you should have these structs

// MARK: - Category
struct Category: Codable {
    let categories: [CategoryElement]
}

// MARK: - CategoryElement
struct CategoryElement: Codable {
    let categories: Categories
}

// MARK: - Categories
struct Categories: Codable {
    let id: Int
    let name: String
}




URLSession.shared.dataTask(with: request) { data, response, error in
    if let data = data {
        do {
            // process data
            if let category = try JSONDecoder().decode(Category.self, from: data) { print(category) }
        } catch  {
            print(error)
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM