大家,如果字段之一未知,如何序列化json结构?
我的 json 是:
{"brands":{"any":false,"19":{"is_all":false,"values":[185,182,178],"include":true},"23":{"is_all":false,"values":[198,199,201],"include":true}},"price":[2000,10000],"year":[1990,2018],"fuel_type":[1,2],"engine_capacity":[\"1.8\",\"4.8\"],"color":[1,2,3],"gearbox_id":[2],"is_georgia":false}
Run Code Online (Sandbox Code Playgroud)
但:
"19":{"is_all":false,"values":[185,182,178],"include":true}
"23":{"is_all":false,"values":[198,199,201],"include":true}
19 和 23 - 是字符串未知值,由 API 生成。
所以我的结构是:
struct auto_order_model: Decodable {
var brands: brand_details <---- this is problem
let price: [Int]
let year: [Int]
let fuel_type: [Int]
let engine_capacity: [String]
let color: [Int]
let gearbox_id: [Int]
let is_georgia: Bool
}
struct brand_details: Decodable {
var any: Bool
var brand_num: [models]?
}
struct models: Decodable {
var is_all: Bool
var values: [Int]
var include: Bool
}
Run Code Online (Sandbox Code Playgroud)
我像这样解码这个json:
do {
let data = …Run Code Online (Sandbox Code Playgroud)