swift - 如何使用 Codable 处理这种 JSON 格式
问题描述
有时“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)
}
}
推荐阅读
- terraform - 在 Terraform 中创建现有 aws_instance 资源的列表
- python - 如何使用字典存储 Python 中方法生成的文件名?
- python - 使用绝对导入运行单个 Python 文件时出现 ModuleNotFoundError
- scala - Spark UDF 没有正确给出滚动计数
- powerbi - 任何可用于替换现有表格并应用所有步骤的工具
- react-native - ReactNative Expo Webpack:添加“plugins”和“resolve”参数发生异常:throw new Error('Callback was already called.');
- python - SettingWithCopyWarning:试图在 DataFrame 中的切片副本上设置值。尝试使用 .loc[row_indexer,col_indexer] = value 代替,
- oracle - 06531-“对未初始化集合的引用”-PLSQL oracle 中的动态矩阵
- linux - 如何根据关键字提取标签内出现的完整文本?
- python - Pandas 数据条件映射最佳实践