JSON 解码器类型不匹配错误
JSON Decoder Type Mismatch Error
我想使用 decodable 解析 JSON,但我收到错误类型不匹配,例如:
Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array but found a dictionary instead."
我的 JSON :
{
"code": 0,
"data": {
"_id": "string",
"title": "string",
"images": [
"string"
],
"shortDesc": "string",
"desc": "string",
"price": 0,
"discountPrice": 0,
"quantity": 0,
"category": {
"name": "string",
"val": "string"
},
"brand": {
"name": "string",
"val": "string"
},
"variants": [
{
"name": "string",
"value": "string",
"quantity": 0,
"variantCode": "string"
}
],
"stockCode": "string",
"updatedDate": "2018-06-05T14:04:51.226Z",
"status": true,
"isDeleted": true,
"isNew": true,
"freeCargo": true,
"createDate": "2018-06-05T14:04:51.226Z",
"note1": "string",
"note2": "string",
"note3": "string",
"shop": {
"name": "string",
"val": "string"
}
},
"error": "string"
}
我的模型:
struct ProductDetail : Decodable {
let code : Int = 0
let error : String = ""
var data : NestedData? = nil
}
嵌套数据:
struct NestedData : Decodable{
let _id : String = ""
let title : String = ""
let images : [String] = []
let shortDesc : String = ""
let desc : String = ""
let price : Int = 0
let discountPrice : Int = 0
let quantity : Int = 0
let updatedDate : String = ""
let status : Bool = false
let isDeleted : Bool = false
let isNew : Bool = false
let freeCargo : Bool = false
let createDate : String = ""
let note1: String = ""
let note2: String = ""
let note3: String = ""
let variants : [variants]? = nil
let brand : brand? = nil
let category :category? = nil
let shop : shop? = nil
}
对象:
struct variants : Decodable{
let name : String
let val : String
let quantity : Int
let variantCode : String
}
struct brand : Decodable{
let name : String
let val : String
}
struct category : Decodable{
let name : String
let val : String
}
struct shop : Decodable{
let name : String
let val : String
}
我不明白为什么会出现错误,控制台说预期的数组但找到了字典,但不幸的是我不明白。
您服务器的响应与上面发布的响应略有不同,或者您有:
let result = try JSONDecoder().decode([ProductDetail].self, from: jsonData)
而不是正确的:
let result = try JSONDecoder().decode(ProductDetail.self, from: jsonData)
我在操场上测试了上面的 JSON 并尝试使用您在代码中发布的结构进行解码并且成功了。如果我在 ProductDetail
周围添加 []
然后我得到了错误。
我认为你的代码没有错误可能是你的错误在First response
所以仔细检查一下
这是您的模特:
import Foundation
struct ProductDetail: Codable {
let code: Int
let data: NestedData?
let error: String?
}
struct NestedData: Codable {
let id, title: String
let images: [String]
let shortDesc, desc: String
let price, discountPrice, quantity: Int
let category:Category
let brand: Brand
let variants: [Variant]
let stockCode, updatedDate: String
let status, isDeleted, isNew, freeCargo: Bool
let createDate, note1, note2, note3: String
let shop: Shop
enum CodingKeys: String, CodingKey {
case id = "_id"
case title, images, shortDesc, desc, price, discountPrice, quantity, category, brand, variants, stockCode, updatedDate, status, isDeleted, isNew, freeCargo, createDate, note1, note2, note3, shop
}
}
struct Brand: Codable {
let name, val: String
}
struct Category: Codable {
let name, val: String
}
struct Shop : Codable{
let name : String
let val : String
}
struct Variant: Codable {
let name, value: String
let quantity: Int
let variantCode: String
}
extension ProductDetail {
init(data: Data) throws {
self = try JSONDecoder().decode(ProductDetail.self, from: data)
}
}
这里如何使用:
switch response.result {
case .success:
guard let data = response.data else { return }
let productDetail = try? ProductDetail.init(data: data){
//
}
}
好的,我建议你使用 quicktype
它将帮助您制作模型
我用它,这是 型号
struct ProductDetail: Codable {
let code: Int?
let data: DataClass?
let error: String?
}
struct DataClass: Codable {
let id, title: String?
let images: [String]?
let shortDesc, desc: String?
let price, discountPrice, quantity: Int?
let category, brand: Brand?
let variants: [Variant]?
let stockCode, updatedDate: String?
let status, isDeleted, isNew, freeCargo: Bool?
let createDate, note1, note2, note3: String?
let shop: Brand?
enum CodingKeys: String, CodingKey {
case id = "_id"
case title, images, shortDesc, desc, price, discountPrice, quantity, category, brand, variants, stockCode, updatedDate, status, isDeleted, isNew, freeCargo, createDate, note1, note2, note3, shop
}
}
struct Brand: Codable {
let name, val: String?
}
struct Variant: Codable {
let name, value: String?
let quantity: Int?
let variantCode: String?
}
之后,你可以使用JSONDecoder
Alamofire.request(urlCourses, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
switch response.result {
case .success:
guard let data = response.data else { return }
do {
let productDetail = try? JSONDecoder().decode(ProductDetail.self, from: jsonData)
} catch let jsonError {
print("Error serializing json:", jsonError)
}
case .failure(let error):
print(error)
}
}
我想使用 decodable 解析 JSON,但我收到错误类型不匹配,例如:
Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array but found a dictionary instead."
我的 JSON :
{
"code": 0,
"data": {
"_id": "string",
"title": "string",
"images": [
"string"
],
"shortDesc": "string",
"desc": "string",
"price": 0,
"discountPrice": 0,
"quantity": 0,
"category": {
"name": "string",
"val": "string"
},
"brand": {
"name": "string",
"val": "string"
},
"variants": [
{
"name": "string",
"value": "string",
"quantity": 0,
"variantCode": "string"
}
],
"stockCode": "string",
"updatedDate": "2018-06-05T14:04:51.226Z",
"status": true,
"isDeleted": true,
"isNew": true,
"freeCargo": true,
"createDate": "2018-06-05T14:04:51.226Z",
"note1": "string",
"note2": "string",
"note3": "string",
"shop": {
"name": "string",
"val": "string"
}
},
"error": "string"
}
我的模型:
struct ProductDetail : Decodable {
let code : Int = 0
let error : String = ""
var data : NestedData? = nil
}
嵌套数据:
struct NestedData : Decodable{
let _id : String = ""
let title : String = ""
let images : [String] = []
let shortDesc : String = ""
let desc : String = ""
let price : Int = 0
let discountPrice : Int = 0
let quantity : Int = 0
let updatedDate : String = ""
let status : Bool = false
let isDeleted : Bool = false
let isNew : Bool = false
let freeCargo : Bool = false
let createDate : String = ""
let note1: String = ""
let note2: String = ""
let note3: String = ""
let variants : [variants]? = nil
let brand : brand? = nil
let category :category? = nil
let shop : shop? = nil
}
对象:
struct variants : Decodable{
let name : String
let val : String
let quantity : Int
let variantCode : String
}
struct brand : Decodable{
let name : String
let val : String
}
struct category : Decodable{
let name : String
let val : String
}
struct shop : Decodable{
let name : String
let val : String
}
我不明白为什么会出现错误,控制台说预期的数组但找到了字典,但不幸的是我不明白。
您服务器的响应与上面发布的响应略有不同,或者您有:
let result = try JSONDecoder().decode([ProductDetail].self, from: jsonData)
而不是正确的:
let result = try JSONDecoder().decode(ProductDetail.self, from: jsonData)
我在操场上测试了上面的 JSON 并尝试使用您在代码中发布的结构进行解码并且成功了。如果我在 ProductDetail
周围添加 []
然后我得到了错误。
我认为你的代码没有错误可能是你的错误在First response
所以仔细检查一下
这是您的模特:
import Foundation
struct ProductDetail: Codable {
let code: Int
let data: NestedData?
let error: String?
}
struct NestedData: Codable {
let id, title: String
let images: [String]
let shortDesc, desc: String
let price, discountPrice, quantity: Int
let category:Category
let brand: Brand
let variants: [Variant]
let stockCode, updatedDate: String
let status, isDeleted, isNew, freeCargo: Bool
let createDate, note1, note2, note3: String
let shop: Shop
enum CodingKeys: String, CodingKey {
case id = "_id"
case title, images, shortDesc, desc, price, discountPrice, quantity, category, brand, variants, stockCode, updatedDate, status, isDeleted, isNew, freeCargo, createDate, note1, note2, note3, shop
}
}
struct Brand: Codable {
let name, val: String
}
struct Category: Codable {
let name, val: String
}
struct Shop : Codable{
let name : String
let val : String
}
struct Variant: Codable {
let name, value: String
let quantity: Int
let variantCode: String
}
extension ProductDetail {
init(data: Data) throws {
self = try JSONDecoder().decode(ProductDetail.self, from: data)
}
}
这里如何使用:
switch response.result {
case .success:
guard let data = response.data else { return }
let productDetail = try? ProductDetail.init(data: data){
//
}
}
好的,我建议你使用 quicktype
它将帮助您制作模型
我用它,这是 型号
struct ProductDetail: Codable {
let code: Int?
let data: DataClass?
let error: String?
}
struct DataClass: Codable {
let id, title: String?
let images: [String]?
let shortDesc, desc: String?
let price, discountPrice, quantity: Int?
let category, brand: Brand?
let variants: [Variant]?
let stockCode, updatedDate: String?
let status, isDeleted, isNew, freeCargo: Bool?
let createDate, note1, note2, note3: String?
let shop: Brand?
enum CodingKeys: String, CodingKey {
case id = "_id"
case title, images, shortDesc, desc, price, discountPrice, quantity, category, brand, variants, stockCode, updatedDate, status, isDeleted, isNew, freeCargo, createDate, note1, note2, note3, shop
}
}
struct Brand: Codable {
let name, val: String?
}
struct Variant: Codable {
let name, value: String?
let quantity: Int?
let variantCode: String?
}
之后,你可以使用JSONDecoder
Alamofire.request(urlCourses, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
switch response.result {
case .success:
guard let data = response.data else { return }
do {
let productDetail = try? JSONDecoder().decode(ProductDetail.self, from: jsonData)
} catch let jsonError {
print("Error serializing json:", jsonError)
}
case .failure(let error):
print(error)
}
}