【发布时间】:2020-07-28 17:26:58
【问题描述】:
我有一个 json,其中一部分不是恒定的,我不知道如何执行 Codable 结构。 请参阅下面的示例代码:
fruits 部分不是恒定的,所以我不确定如何执行 Codable 结构。我试图在 SOF 上找到答案,但找不到“非恒定 json Codable 结构”的答案。我可能没有搜索到正确的关键词。
谢谢。
这里是json的例子:
{
"success": true,
"username": "app",
"data": {
"locations": {
"asia": {
"japan": {
"store_count": 5
},
"korea": {
"store_count": 3
}
}
},
"market": {
"fruits": {
"banana": {
"price": 50.00,
"count": 2
},
"apple": {
"price": 444.00,
"count": 16
},
"mango": {
"price": 28.00,
"count": 1
},
"peach": {
"price": 50.00,
"count": 2
},
"watermelon": {
"price": 50.00,
"count": 2
},
"blackberry": {
"price": 57.00,
"count": 2
}
}
}
}
json 结构
struct Markets: Codable {
let success: Bool?
let data: Data?
struct Data: Codable {
let locations: Locations
let market: Market
struct Locations: Codable {
let asia: Asia
struct Asia: Codable {
let japan: Stores
ler korea:
}
struct Stores: Codable {
let store_count: Int
}
}
struct Market: Codable {
var fruits: Type
struct Type: Codable {
// the fruits type are not constant.
}
}
}
}
}
【问题讨论】: