首页 > 解决方案 > 使用可解码协议解析 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”……在这种情况下,我可以创建确认可解码的结构还是我需要手动解析它?

请指教

标签: jsonswiftstructdecodable

解决方案


对于第一个问题,您有一个名为的键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 {

}

推荐阅读