有时“ box”数组包含一些时间为空的字典(“ box”:[])。我尝试了很多次,但每次尝试都失败,并显示此错误。
错误::由于格式不正确,无法读取数据。
{
"id": 57,
"title": "PRIVATE SET :: Royal Blue Colour",
"description": "3 of your selected Fragrances from our Classic Line in an overwhelmingly unique Royal Blue wooden box.",
"volume": "",
"price": 0,
"sub-category": "Assemblage",
"image": "http://192.168.1.163/ramasaat/public/storage/images/Products/e0dc5e9d15704ccac5c0965e9e2c782e.jpg",
"related": [
{
"id": 55,
"title": "ELITE SET :: Royal Blue Colour",
"description": "8 of your selected Fragrances from our Classic Line in an overwhelmingly unique Royal Blue wooden box.",
"volume": "",
"price": 0,
"category": "Assemblage",
"sub-category": "Assemblage",
"image": "http://192.168.1.163/ramasaat/public/storage/images/Products/deb955bd41baac2e98ee026c28734fb1.jpg"
},
.
.
.
],
"num_item": 3,
===============> "box": [
{
"id": 1,
"title": "MAYYAS",
"description": "An unmistakable presence and a charming aura of musk with an oriental flair. This fragrance merges a mixture of contrasting scents flawlessly and is full of mystery and charisma.",
"price": 990,
"image": "http://192.168.1.163/ramasaat/public/storage/images/Products/07c44b5a8fea99804ac57520103f755e.jpg"
},
.
.
.
.
]
}
像这样使用
struct RootClass : Codable {
let box : [Box]?
let descriptionField : String?
let id : Int?
let image : String?
let numItem : Int?
let price : Int?
let related : [Related]?
let subcategory : String?
let title : String?
let volume : String?
enum CodingKeys: String, CodingKey {
case box = "box"
case descriptionField = "description"
case id = "id"
case image = "image"
case numItem = "num_item"
case price = "price"
case related = "related"
case subcategory = "sub-category"
case title = "title"
case volume = "volume"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
box = try values.decodeIfPresent([Box].self, forKey: .box)
descriptionField = try values.decodeIfPresent(String.self, forKey: .descriptionField)
id = try values.decodeIfPresent(Int.self, forKey: .id)
image = try values.decodeIfPresent(String.self, forKey: .image)
numItem = try values.decodeIfPresent(Int.self, forKey: .numItem)
price = try values.decodeIfPresent(Int.self, forKey: .price)
related = try values.decodeIfPresent([Related].self, forKey: .related)
subcategory = try values.decodeIfPresent(String.self, forKey: .subcategory)
title = try values.decodeIfPresent(String.self, forKey: .title)
volume = try values.decodeIfPresent(String.self, forKey: .volume)
}
}
struct Related : Codable {
let category : String?
let descriptionField : String?
let id : Int?
let image : String?
let price : Int?
let subcategory : String?
let title : String?
let volume : String?
enum CodingKeys: String, CodingKey {
case category = "category"
case descriptionField = "description"
case id = "id"
case image = "image"
case price = "price"
case subcategory = "sub-category"
case title = "title"
case volume = "volume"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
category = try values.decodeIfPresent(String.self, forKey: .category)
descriptionField = try values.decodeIfPresent(String.self, forKey: .descriptionField)
id = try values.decodeIfPresent(Int.self, forKey: .id)
image = try values.decodeIfPresent(String.self, forKey: .image)
price = try values.decodeIfPresent(Int.self, forKey: .price)
subcategory = try values.decodeIfPresent(String.self, forKey: .subcategory)
title = try values.decodeIfPresent(String.self, forKey: .title)
volume = try values.decodeIfPresent(String.self, forKey: .volume)
}
}
struct Box : Codable {
let descriptionField : String?
let id : Int?
let image : String?
let price : Int?
let title : String?
enum CodingKeys: String, CodingKey {
case descriptionField = "description"
case id = "id"
case image = "image"
case price = "price"
case title = "title"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
descriptionField = try values.decodeIfPresent(String.self, forKey: .descriptionField)
id = try values.decodeIfPresent(Int.self, forKey: .id)
image = try values.decodeIfPresent(String.self, forKey: .image)
price = try values.decodeIfPresent(Int.self, forKey: .price)
title = try values.decodeIfPresent(String.self, forKey: .title)
}
}