使用ObjectMapper iOS有效地解析JSON数据

问题描述 投票:-1回答:1

嗨,我有一个给定格式的JSON

"exclusions": [
    [
      {
        "facility_id": "1",
        "options_id": "4"
      },
      {
        "facility_id": "2",
        "options_id": "6"
      }
    ],
    [
      {
        "facility_id": "1",
        "options_id": "3"
      },
      {
        "facility_id": "3",
        "options_id": "12"
      }
    ],
    [
      {
        "facility_id": "2",
        "options_id": "7"
      },
      {
        "facility_id": "3",
        "options_id": "12"
      }
    ]
  ]

我正在使用Object Mapper库解析JSON,但据我所知,我觉得它缺少一个键,因为关键exclusions下的每个对象都是一个数组,无论如何我可以使用ObjectMapper解析它

ios json swift objectmapper
1个回答
1
投票

为什么不Codable

class Root:Codable {
  let exclusions:[[InnerItem]]
}
class InnerItem:Codable {
   let facilityId:String
   let optionsId:String
  private enum CodingKeys: String, CodingKey {
     case facilityId = "facility_id"
     case optionsId = "options_id"
  }
}

//

do {
     let decoded = try JSONDecoder().decode(Root.self, from:jsonData)
     print(decoded)
} catch {
    print(error)
}

顺便说一句,你的json需要一个周围的{}

© www.soinside.com 2019 - 2024. All rights reserved.