ios - 根目录下的 Codable Handle 动态键
问题描述
我已经看过几个类似的问题,但我有不同的 JSON
所以我的 JSON 如下所示
var json = """
{
"Array1": [
{
"FinancialYear": "17-18"
}],
"Array2": [
{
"FinancialYear": "17-18"
}]
}
"""
问题是Array1和Array2键,它们似乎是动态的,它位于 ROOT 并且可能更像 Array3、Array4 等
我想使用 Codable 但由于根 (Array1,Array2) 的动态键我无法摆脱它。
这是我尝试过但不工作的结构
struct CodableJSON: Codable {
var response:[String:[ArrayInside]]
enum CodingKeys: String, CodingKey {
case response = "What should I write here ?" // What should be here ?
}
}
解决方案
在这种情况下,只声明ArrayInside
结构
struct ArrayInside: Decodable {
...
}
并将根对象解码为字典
let result = try JSONDecoder().decode([String:[ArrayInside]].self, from: data)
推荐阅读
- html - 如何通过按 shift + enter 来制作新的行输入消息?角
- c# - 警告:SQL 注入攻击
- angular - Angular - 重置其值后无法将数据发送到子组件
- python - 从 csv 读取日期
- python - pyqtgraph GraphicsLayout中的setData不起作用
- asp.net-core - ExplicitExpansion() 导致 OData 扩展不起作用
- java - 无法使用循环合并图像 Java
- database - flask-sqlalchemy 慢分页计数
- node.js - 使用for each in node js sequelize将数据存储在数组中
- laravel - Laravel - 按日期排序帖子并通过分页获取它们