尝试使用 Swift json 解码器解析 json 时出现类型不匹配错误。
我只是找不到正常解析它的方法。
错误:
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))
解码器代码:
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()
}
}
和课程:
第一的:
class ResultDetails : Decodable
{
let data : DataDetails
init(data : DataDetails) {
self.data = data
}
}
第二:
class DataDetails : Decodable
{
let itemArr: ItemArr
init(itemArr: ItemArr) {
self.itemArr = itemArr
}
}
第三:
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
}
我想我在过去 6 小时内尝试了所有方法来修复它,请帮忙!
编辑:
我放错了三等舱。现在是正确的