json - 使用可解码协议解析 JSON
问题描述
我有下面的json,我想从中解析/分配值
{
"Rooms":[
{
"id":"100",
"title":"CS Classroom",
"description":"Classroom for Computer science students",
"capacity":"50"
},
{
"id":"101",
"title":"Mechanical Lab",
"description":"Mechanical Lab work",
"capacity":"50"
},
{
"id":"108",
"title":"Computer Lab",
"description":"Computer Lab work",
"capacity":"50"
}
]
}
这个 json 是 [Dictionary: Dictonary] 类型,它只有键“Rooms”
在创建结构时我应该创建
struct RoomsInfo: Decodable {
let rooms: Rooms
}
struct Rooms {
let id: String
let title: String
let description: String
let capacity: String
}
我的第一个问题是:由于我只有 Rooms 键,是否有可能只创建一个 struct 而不是两个?
我的第二个问题是:如果我的 json 的键为“Rooms1”、“Rooms2”、“Rooms3”、“Rooms4”……在这种情况下,我可以创建确认可解码的结构还是我需要手动解析它?
请指教
解决方案
对于第一个问题,您有一个名为的键Room
,因此它必须解码该键,是否可以不确定,而不是JSON
先解析该数据,调用该键的值JSON["Rooms"]
,然后将其中的内容解析为 a [Room].self
,
对于第二个问题,如果计数是无限的,就好像您不知道Room
键计数将是多少,那么Decoder
能力就会受到限制,但是您始终可以将值映射为Dictionary
,然后将值解码为Room
而无需关心关键,这个技巧会做,但你会放弃原来的Key
。
第二种情况的更新:查看下面的代码。
typealias jsonDictionary = [String: Any]
let jsonData = json.data(using: .utf8)! // converting test json string to data
var arrayOfRooms: [Room] = []
do {
let serialized = try JSONSerialization.jsonObject(with: jsonData, options: []) // serializing jsonData to json object
if let objects = serialized as? [String: Any] { //casting to dictionary
for key in objects.keys { //looping into the keys rooms (n) number
let rooms = objects[key] // getting those rooms by key
let data = try JSONSerialization.data(withJSONObject: rooms!, options: []) //converting those objects to data again to parse
var myRoom = try! JSONDecoder().decode([Room].self, from: data) // decoding each array of rooms
arrayOfRooms.append(contentsOf: myRoom) // appending rooms to main rooms array declared on top
print("Data", data) // just to check
}
print("MY Array Of Rooms Count \(arrayOfRooms.count)")
} else {
print("nil")
}
} catch {
}
推荐阅读
- twitter-bootstrap - 什么是让 bootstrap 4 工作的正确方法
- c++ - 具有相同大小和内容的 C++ 字符串比较返回 false
- angular - Ionic - Angular - Ionic-native facebook:预期 2 个参数,但得到 4 个
- java - 如何使用 cxf-codegen-plugin 从 wsdl 在 java 中生成区分大小写的字段?
- c++ - std::mutex 使用示例
- python-2.7 - 如何在 pyspark 或 python 中的数据库 upserts 中进行并行处理
- python - 当函数将数组作为参数时,如何使用 Numba 对函数进行矢量化?
- javascript - 使用 karma-jasmine 测试规范中的对象属性更改
- javascript - 使用 startsWith 函数识别邮政编码的前三位
- linux-kernel - 用可加载的模块替换内核内置模块