【问题标题】:Parse a JSON data efficiently using ObjectMapper iOS使用 ObjectMapper iOS 高效解析 JSON 数据
【发布时间】:2018-09-17 19:02:59
【问题描述】:

您好,我有一个给定格式的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 解析它

【问题讨论】:

  • 你能举一个你试过的例子吗?完成解析后,您希望它看起来像什么?
  • 现在不要使用 ObjectMapper Codable 可用

标签: ios json swift objectmapper


【解决方案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 需要一个环绕 {}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多