JSON 使用 Decodable 协议解析

JSON Parsing using Decodable protocol

下面有 json,我想从

中获取 parse/assign 个值
{
"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 键,是否可以只创建一个结构而不是两个?

我的第二个问题是:如果我的 json 的键为 "Rooms1"、"Rooms2"、"Rooms3"、"Rooms4"... 在这种情况下怎么办我可以创建确认可解码的结构还是需要手动解析它?

请指教

对于第一个问题,您有一个名为 Room 的密钥,因此它必须解码该密钥, 是否有可能不确定,而不是解析 JSON 数据首先调出该键 JSON["Rooms"] 的值,然后将其中的内容解析为 [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 {

}

答案 #1:是的,nestedContainers 是可能的,但努力大于收益。

答案 #2:将字典解码为 [String:Room] 或使用

中描述的自定义编码键