Search code examples
jsonswiftjsondecoder

URLSession Type Mismatch Error


I'm getting an Type Mismatch error when trying to parse json with Swift jSon Decoder.

I just can't find a way to parse it normally.

The Error:

typeMismatch(Swift.String, Swift.DecodingError.Context(codingPath:
[CodingKeys(stringValue: "data", intValue: nil), 
CodingKeys(stringValue: "itemArr", intValue: nil), 
CodingKeys(stringValue: "price", intValue: nil)], debugDescription:
"Expected to decode String but found a number instead.",
underlyingError:
nil))

The Decoder Code:

 func getDealDetails(id : String ,completion : @escaping ()->())
{
    let jsonUrl = "https://androidtest.inmanage.com/api/1.0/android/getDeal_\(id).txt"
    guard let url = URL(string: jsonUrl) else { return }

    URLSession.shared.dataTask(with: url) { (data, response, error) in

        guard let data = data else { return }

        do
        {
            let deal = try JSONDecoder().decode(ResultDetails.self, from: data)
            AppManager.shared.dealToShow = deal.data.itemArr

        }catch
        {
            print("There's an error: \(error)")
        }

        completion()

        }.resume()



}
}

And The Classes:

First:

class ResultDetails : Decodable
{
  let data : DataDetails

init(data : DataDetails) {
    self.data = data
}


}

Second:

class DataDetails : Decodable
{
 let itemArr: ItemArr

init(itemArr: ItemArr) {
    self.itemArr = itemArr
}
}

And Third:

class ItemArr : Decodable
{
let id, title, price, description: String
let image: String
let optionsToShow: Int
let gps: Gps
let website, phone: String

  init(id: String, title: String, price: String, description: String,   image: String, optionsToShow: Int, gps: Gps, website: String, phone: String) {
    self.id = id
    self.title = title
    self.price = price
    self.description = description
    self.image = image
    self.optionsToShow = optionsToShow
    self.gps = gps
    self.website = website
    self.phone = phone
}

I think I tried everything for the last 6 hours to fix it, please help!

EDIT:

I put a wrong third class. now it's the right one


Solution

  • Yes you have an error

    let price: Int should be let price: String because it string in Json "price": "896" -- > String

    Example for Int "optionsToShow":1 --> this is Int

    Here is complete code you can use

    import Foundation
    struct ResultDetails: Codable {
        let data: DataDetails
    }
    
    struct DataDetails: Codable {
        let itemArr: ItemArr
    }
    
    struct ItemArr: Codable {
        let id, title, price, description: String
        let image: String
        let optionsToShow: Int
        let gps: Gps
        let website, phone: String
    }
    
    struct Gps: Codable {
        let lat, lon: String
    }
    // MARK: Convenience initializers
    
    extension ResultDetails {
        init(data: Data) throws {
            self = try JSONDecoder().decode(ResultDetails.self, from: data)
        }
    
        init(_ json: String, using encoding: String.Encoding = .utf8) throws {
            guard let data = json.data(using: encoding) else {
                throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
            }
            try self.init(data: data)
        }
    
    
        func jsonData() throws -> Data {
            return try JSONEncoder().encode(self)
        }
    
        func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
            return String(data: try self.jsonData(), encoding: encoding)
        }
    }