【发布时间】:2021-11-14 07:01:12
【问题描述】:
以下代码仅解析数组中的一个对象,但在 json 响应中有两个对象。 我不明白为什么这段代码只解析一个对象而不是另一个。当下面的代码解析动态键名称为“40”的第二个对象时,我得到了 nil 值。
Json 结构
这是我想使用 Codable 类解析的 json 结构。
{
"search_result": "",
"related_product_price_info": [
{
"39": {
"price": 1000.0,
"discount_percentage": 10.0,
"related_product_group_id": 1039,
"discounted_price": 900.0
}
},
{
"40": {
"price": 999.0,
"discount_percentage": 10.0,
"related_product_group_id": 1040,
"discounted_price": 899.1
}
}
]
}
模型
struct ProductSearchResult: Codable {
let searchResult: String?
let relatedProductPriceInfo: [RelatedProductPriceInfo]?
enum CodingKeys: String, CodingKey {
case searchResult = "search_result"
case relatedProductPriceInfo = "related_product_price_info"
}
}
struct RelatedProductPriceInfo: Codable {
var relatedProductPrice: RelatedProductPrice?
private struct DynamicCodingKeys: CodingKey {
var stringValue: String
init?(stringValue: String) {
self.stringValue = stringValue
}
var intValue: Int?
init?(intValue: Int) {
return nil
}
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: DynamicCodingKeys.self)
for key in container.allKeys {
guard let decodedObject = try? container.decode(RelatedProductPrice.self, forKey: DynamicCodingKeys(stringValue: key.stringValue)!) else{
continue
}
relatedProductPrice = decodedObject
}
}
}
}
解析
let result = try? JSONDecoder().decode(ProductSearchResult.self, from: data)
输出
我只能解析一个对象 (key = "39") 而不是另一个 (key="40")
▿ Optional<ProductSearchResult>
▿ some : ProductSearchResult
- searchResult : ""
▿ relatedProductPriceInfo : Optional<Array<RelatedProductPriceInfo>>
▿ some : 2 elements
▿ 0 : RelatedProductPriceInfo
▿ relatedProductPrice : Optional<RelatedProductPrice>
▿ some : RelatedProductPrice
- price : 1000
- discountPercentage : 10
- relatedProductGroupID : 1039
- discountedPrice : 900
▿ 1 : RelatedProductPriceInfo
- relatedProductPrice : nil
【问题讨论】:
-
更改为
let relatedProductPriceInfo: [String: RelatedProductPriceInfo]并删除所有多余的东西。 -
不,它不起作用。我通过这种方式得到了
result = nil。 @Joakim Danielson -
是的,我有点快,
let relatedProductPriceInfo: [[String: RelatedProductPrice]] -
没有还是一样的
result = nil -
它对我来说很好用。一般建议使用
try而不是try?并避免可选属性,您可以通过使用do {...} catch { print(error) }打印任何错误来获得很多帮助
标签: json swift parsing dynamic key