gpt4 book ai didi

json - 如何解码可变类型的 JSON 数据字段?

转载 作者:行者123 更新时间:2023-11-28 05:52:41 24 4
gpt4 key购买 nike

我正在从 API 接收 JSON 数据,但有些字段有时是字符串,有时是整数。此类问题的最佳解决方案是什么?

这是我的解码代码:

public struct Nutriments {
public let energy: String?
public let energyServing: String?
public let energy100g: String?

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
energy = try container.decodeIfPresent(String.self, forKey: .energy)
energy100g = try container.decodeIfPresent(String.self, forKey: .energy100g)
energyServing = try container.decodeIfPresent(String.self, forKey: .energyServing)
}
}

JSON 示例:

"nutriments": {
"energy_100g": 8.97,
"energy_serving": "55",
"energy": "7"
}

其他时候是这样的:

"nutriments": {
"energy_100g": "8.97",
"energy_serving": 55,
"energy": 7
}

最佳答案

首先责怪服务的所有者发送了不一致的数据。

要解码这两种类型,您可以在 init 方法中检查类型。

至少 API 似乎发送了所有键,因此您可以将所有结构成员声明为非可选

public struct Nutriments {

public let energy: String
public let energyServing: String
public let energy100g: String

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
do { energy = try container.decode(String.self, forKey: .energy) }
} catch DecodingError.typeMismatch { energy = String(try container.decode(Int.self, forKey: .energy)) }
do { energy100g = try container.decode(String.self, forKey: .energy100g) }
} catch DecodingError.typeMismatch { energy100g = String(try container.decode(Int.self, forKey: .energy100g)) }
do { energyServing = try container.decode(String.self, forKey: .energyServing) }
} catch DecodingError.typeMismatch { energyServing = String(try container.decode(Int.self, forKey: .energyServing)) }
}
}

关于json - 如何解码可变类型的 JSON 数据字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52344272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com