json - 如何解码可变类型的 JSON 数据字段?
问题描述
我从 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)) }
}
}
推荐阅读
- c# - 用值初始化字典(C#)
- python - 正则表达式在所有标点符号和英文字符序列处拆分,并在 Python 中使用注音保留分隔符
- javascript - 获取 HTML 内容下的 href 列表
- python - 有没有办法在网站上嵌入使用 python 的 Networkx 包的图形代码,以便输出可以直接显示在网站上?
- r - 利用回收将不同长度的列表转换为数据帧的功能
- node.js - 导出的函数在另一个文件 nodejs 中不起作用
- ios - 如何从函数内部获取文本字段的文本?
- .net - 使用 DebugDiag 调试挂起 - GC 挂起?
- java - 接受许可后如何运行相机
- javascript - 交替执行功能