如何使用swift解码协议解析嵌套的JSON数据?
how to use swift decodable protocol to parse nested JSON data?
我有一个应用程序可以从服务器获取一些 JSON 数据,看起来像这样:
"operationsInfo": {
"bill": {
"activeProviders": [
{
"max": 50,
"min": 10,
"name": "name1"
},
{
"max": 50,
"min": 10,
"name": "name2"
}
]
},
"pin": {
"activeProviders": [
{
"max": 50,
"min": 10,
"name": "name3"
},
{
"max": 50,
"min": 10,
"name": name4
}
]
}
}
如何使用 swift 可解码协议反序列化此 JSON 数据?
我的自定义对象如下所示:
struct operationList: Decodable {
let name: String
let action: String
let min, max: Int
}
operationList 对象中的操作值必须等于 "bill" 或 "pin"。最后我想在解码JSON数据时得到一个operationList对象类型的数组,例如:
let operationListArray = [operationList1, operationList2, operationList3, operationList4]
operationList1.action = "bill", operationList1.max = 50, operationList1.name = "name1"
operationList2.action = "bill", operationList2.max = 50, operationList2.name = "name2"
operationList3.action = "pin", operationList3.max = 50, operationList3.name = "name3"
operationList4.action = "pin", operationList4.max = 50, operationList4.name = "name4"
我已经看到像这样的其他答案,例如:
但我的问题是如何将 "bill" 或 "pin" 放入操作值中,将来可能会出现新的键值,如 "transfer"(如 "pin" 或 "bill") 添加到 JSON 数据。
正如@rmaddy 在评论中提到的,您想分两步完成此操作。
首先,创建一个与您的 JSON 格式匹配的结构并对其进行解码:
struct Response: Decodable {
let operationsInfo: [String: Providers]
}
struct Providers: Decodable {
let activeProviders: [Provider]
}
struct Provider: Decodable {
let name: String
let min: Int
let max: Int
}
let response = try JSONDecoder().decode(Response.self, from: data)
然后,声明一个表示您希望数据采用的格式的结构并将其映射:
struct ProviderAction {
let action: String
let provider: Provider
}
let actions: [ProviderAction] = response.operationsInfo.map { action, providers in
providers.activeProviders.map { ProviderAction(action: action, provider: [=11=]) }
}.flatMap { [=11=] }
我有一个应用程序可以从服务器获取一些 JSON 数据,看起来像这样:
"operationsInfo": {
"bill": {
"activeProviders": [
{
"max": 50,
"min": 10,
"name": "name1"
},
{
"max": 50,
"min": 10,
"name": "name2"
}
]
},
"pin": {
"activeProviders": [
{
"max": 50,
"min": 10,
"name": "name3"
},
{
"max": 50,
"min": 10,
"name": name4
}
]
}
}
如何使用 swift 可解码协议反序列化此 JSON 数据? 我的自定义对象如下所示:
struct operationList: Decodable {
let name: String
let action: String
let min, max: Int
}
operationList 对象中的操作值必须等于 "bill" 或 "pin"。最后我想在解码JSON数据时得到一个operationList对象类型的数组,例如:
let operationListArray = [operationList1, operationList2, operationList3, operationList4] operationList1.action = "bill", operationList1.max = 50, operationList1.name = "name1" operationList2.action = "bill", operationList2.max = 50, operationList2.name = "name2" operationList3.action = "pin", operationList3.max = 50, operationList3.name = "name3" operationList4.action = "pin", operationList4.max = 50, operationList4.name = "name4"
我已经看到像这样的其他答案,例如:
正如@rmaddy 在评论中提到的,您想分两步完成此操作。
首先,创建一个与您的 JSON 格式匹配的结构并对其进行解码:
struct Response: Decodable {
let operationsInfo: [String: Providers]
}
struct Providers: Decodable {
let activeProviders: [Provider]
}
struct Provider: Decodable {
let name: String
let min: Int
let max: Int
}
let response = try JSONDecoder().decode(Response.self, from: data)
然后,声明一个表示您希望数据采用的格式的结构并将其映射:
struct ProviderAction {
let action: String
let provider: Provider
}
let actions: [ProviderAction] = response.operationsInfo.map { action, providers in
providers.activeProviders.map { ProviderAction(action: action, provider: [=11=]) }
}.flatMap { [=11=] }