当我试图用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个小时里尝试了所有的方法来修复它,请帮帮忙!
编辑:
我放错了三等舱。现在它是正确的
发布于 2018-05-11 16:32:43
是的,你有一个错误
let price: Int应该是let price: String,因为它在Json "price": "896" -- > string中的字符串
Int "optionsToShow":1的示例-->这是Int
这里是您可以使用的完整代码
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)
}
}发布于 2018-05-11 16:34:44
错误信息是错误的。根据JSON链接和类,它应该是
CodingKeys(stringValue:"price",intValue: nil),debugDescription:希望解码Int,但是找到了一个字符串/数据。,
但是,它将确切地告诉您出了什么问题:键price的值是String而不是Int。
您必须仔细阅读JSON。格式非常简单。例如,everything在双引号中是String,没有例外。
数据结构太复杂了。使用结构并删除初始化器。顺便说一句,这里有一个错误的ItemsArr和ItemArr,并且没有键orderNum。
密钥image可以被解码为URL。这就足够了
struct ResultDetails : Decodable {
let data : DataDetails
}
struct DataDetails : Decodable {
let itemArr: ItemArr
}
struct ItemArr : Decodable {
let id, title: String
let price: String
let image: URL
// let orderNum: Int
}仅在要映射密钥时才指定CodingKeys。在您的情况下,您甚至可以省略CodingKeys,而是使用convertFromSnakeCase策略。
decoder.keyDecodingStrategy = .convertFromSnakeCasehttps://stackoverflow.com/questions/50296358
复制相似问题