下面的代碼只決議了陣列中的一個物件,但是在json回應中卻有兩個物件。 我不明白為什么這段代碼只決議了一個物件而不是另一個。當下面的代碼決議第二個物件時,我得到了零值,這個物件的動態鍵名是 "40"。
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[/span
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{
繼續。
}
relatedProductPrice = decodedObject
}
}
}
}
決議
let result = try? JSONDecoder().decode(ProductSearchResult.self, from: data)
輸出
我只能決議一個物件(key="39"),但不能決議另一個(key="40")
? Optional<ProductSearchResult>
? some : ProductSearchResult ProductSearchResult ?
- searchResult : ""
? relatedProductPriceInfo : Optional<Array<RelatedProductPriceInfo>>
? some : 2元素
? 0 : RelatedProductPriceInfo
? relatedProductPrice : Optional<RelatedProductPrice>
? some : RelatedProductPrice RelatedProductPrice ?
- 價格。1000
- discountPercentage : 10
- relatedProductGroupID : 1039
- discountedPrice : 900
? 1 : RelatedProductPriceInfo
- relatedProductPrice : nil
uj5u.com熱心網友回復:
你的資料結構不是很正確。 relatedProductPriceInfo的主體實際上是一個字典的陣列。 如果你正確地映射它,你可以大大簡化事情,而且如果你在解碼器上設定了密鑰解碼選項,你可以避免宣告所有的 CodingKeys。
作為一個最基本的解決方案(你應該更好地捕獲錯誤等)
struct ProductSearchResult: Codable {
let searchResult: String?
let relatedProductPriceInfo: [[String: PriceDetail ]]
}
struct PriceDetail: Codable {
let price: Double[/span
let discountPercentage: Double 折扣百分比: let
let relatedProductGroupId: Double
let discountedPrice: Double
}
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let data = Data(JSON.utf8)
let output = try decoder.decode(ProductSearchResult.self, from: data)
//quick and dirty printing of output。
output.relatedProductPriceInfo.forEach{print($0.key, $0.values)}。
這就給出了一個輸出關
["39"] [__lldb_expr_70.PriceDetail(price: 1000.0, discountPercentage: 10.0, relatedProductGroupId: 1039.0, discountedPrice: 900.0)]
["40"] [__lldb_expr_70.PriceDetail(價格。999.0, discountPercentage: 10.0, relatedProductGroupId: 1040.0, discountedPrice: 899.1)]。
在現實世界中,你可能希望將字典陣列扁平化,或者使用一個中間結構來包裝它們,這都取決于你對各種資料位的使用情況。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/322783.html
標籤:
