我从两个不同的端点获取数据。一个端点返回如下命令:
{
"price":"123.49",
"quantity":"4",
"id":"fkuw-4834-njk3-4444",
"highPrice":"200",
"lowPrice":"100"
}
,另一个端点返回如下的顺序:
{
"p":"123.49", //price
"q":"4", //quantity
"i":"fkuw-4834-njk3-4444" //id
}
我想使用相同的Codable结构来解码两个JSON响应。我该怎么办?是否可以使用一个结构来实现,或者我必须创建第二个结构?以下是第一个JSON返回结构的结构:
struct SimpleOrder:Codable{
var orderPrice:String
var orderQuantity:String
var id:String
var highPrice:String
private enum CodingKeys: String, CodingKey {
case orderPrice = "price"
case orderQuantity = "quantity"
case id
case highPrice
}
}
答案 0 :(得分:3)
无需为Codable结构创建自定义初始值设定项,您只需要使属性可选。我建议创建一个只读计算属性,它将使用零合并运算符返回价格和数量,因此它将始终返回一个或另一个:
struct Order: Codable {
let price: String?
let quantity: String?
let id: String?
let highPrice: String?
let lowPrice: String?
let p: String?
let q: String?
let i: String?
}
extension Order {
var orderPrice: Double {
return Double(price ?? p ?? "0") ?? 0
}
var orderQuantity: Int {
return Int(quantity ?? q ?? "0") ?? 0
}
var userID: String {
return id ?? i ?? ""
}
}
测试:
let ep1 = Data("""
{
"price":"123.49",
"quantity":"4",
"id":"fkuw-4834-njk3-4444",
"highPrice":"200",
"lowPrice":"100"
}
""".utf8)
let ep2 = Data("""
{
"p":"123.49",
"q":"4",
"i":"fkuw-4834-njk3-4444"
}
""".utf8)
do {
let order = try JSONDecoder().decode(Order.self, from: ep2)
print(order.orderPrice) // "123.49\n"
print(order.orderQuantity) // "4\n"
print(order.userID) // "fkuw-4834-njk3-4444\n"
} catch {
print(error)
}
答案 1 :(得分:2)
您可以这样做,但您必须将所有属性声明为可选,并编写自定义初始值设定项
struct SimpleOrder : Decodable {
var orderPrice : String?
var orderQuantity : String?
var id : String?
var highPrice : String?
private enum CodingKeys: String, CodingKey {
case orderPrice = "price"
case orderQuantity = "quantity"
case id
case highPrice
case i, q, p
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
orderPrice = try container.decodeIfPresent(String.self, forKey: .orderPrice)
orderPrice = try container.decodeIfPresent(String.self, forKey: .p)
orderQuantity = try container.decodeIfPresent(String.self, forKey: .orderQuantity)
orderQuantity = try container.decodeIfPresent(String.self, forKey: .q)
id = try container.decodeIfPresent(String.self, forKey: .id)
id = try container.decodeIfPresent(String.self, forKey: .i)
highPrice = try container.decodeIfPresent(String.self, forKey: .highPrice)
}
}
或者使用两个不同的密钥集,检查其中一个密钥的出现并选择适当的密钥集。好处是price
,quantity
和id
可以声明为非可选
struct SimpleOrder : Decodable {
var orderPrice : String
var orderQuantity : String
var id : String
var highPrice : String?
private enum CodingKeys: String, CodingKey {
case orderPrice = "price"
case orderQuantity = "quantity"
case id
case highPrice
}
private enum AbbrevKeys: String, CodingKey {
case i, q, p
}
init(from decoder: Decoder) throws {
let cContainer = try decoder.container(keyedBy: CodingKeys.self)
if let price = try cContainer.decodeIfPresent(String.self, forKey: .orderPrice) {
orderPrice = price
orderQuantity = try cContainer.decode(String.self, forKey: .orderQuantity)
id = try cContainer.decode(String.self, forKey: .id)
highPrice = try cContainer.decode(String.self, forKey: .highPrice)
} else {
let aContainer = try decoder.container(keyedBy: AbbrevKeys.self)
orderPrice = try aContainer.decode(String.self, forKey: .p)
orderQuantity = try aContainer.decode(String.self, forKey: .q)
id = try aContainer.decode(String.self, forKey: .i)
}
}
}