【问题标题】:Codable HAL JSON types可编码的 HAL JSON 类型
【发布时间】:2020-07-28 14:31:49
【问题描述】:

有人有管理 HAL 类型 JSON 数据的流程吗?我遇到的问题是所有数据请求都将返回一个将其实际类型嵌入到“_embedded”键中的容器。我正在努力弄清楚如何从这种类型中解码出来,因为每个嵌入式键都可能有多个 [Any HalTypes] 分配给它。例如,如果我请求菜单项或菜单类别,它将返回相同的总体结构。以下 JSON 用于菜单类别。

例如,

型号

// This file was generated from JSON Schema using quicktype, do not modify it directly.
// To parse the JSON, add this file to your project and do:
//
//   let category = try? newJSONDecoder().decode(Category.self, from: jsonData)

import Foundation

// MARK: - Category
struct Category: Codable {
    var embedded: Embedded?
    var links: CategoryLinksClass?
    var count, limit: Int?

    enum CodingKeys: String, CodingKey {
        case embedded = "_embedded"
        case links = "_links"
        case count, limit
    }
}

// MARK: - Embedded
struct Embedded: Codable {
    var categories: [CategoryElement]?
}

// MARK: - CategoryElement
struct CategoryElement: Codable {
    var links: CategoryLinks?
    var id: String?
    var level: Int?
    var name, posid: String?

    enum CodingKeys: String, CodingKey {
        case links = "_links"
        case id, level, name
        case posid = "pos_id"
    }
}

// MARK: - CategoryLinks
struct CategoryLinks: Codable {
    var linksSelf: Next?

    enum CodingKeys: String, CodingKey {
        case linksSelf = "self"
    }
}

// MARK: - Next
struct Next: Codable {
    var href: String?
    var type: String?
}

// MARK: - CategoryLinksClass
struct CategoryLinksClass: Codable {
    var next, linksSelf: Next?

    enum CodingKeys: String, CodingKey {
        case next
        case linksSelf = "self"
    }
}

JSON

{
  "_embedded": {
    "categories": [
      {
        "_links": {
          "self": {
            "href": "https://api.omnivore.io/1.0/locations/iE7e78GT/menu/categories/1001/",
            "type": "application/json; name=menu_category"
          }
        },
        "id": "1001",
        "level": 0,
        "name": "Entree",
        "pos_id": "1001"
      },
      {
        "_links": {
          "self": {
            "href": "https://api.omnivore.io/1.0/locations/iE7e78GT/menu/categories/1002/",
            "type": "application/json; name=menu_category"
          }
        },
        "id": "1002",
        "level": 0,
        "name": "Appetizer",
        "pos_id": "1002"
      }
    ]
  },
  "_links": {
    "next": {
      "href": "https://api.omnivore.io/1.0/locations/iE7e78GT/menu/categories/?limit=2&start=2",
      "type": "application/json; name=menu_category_list"
    },
    "self": {
      "href": "https://api.omnivore.io/1.0/locations/iE7e78GT/menu/categories/?limit=2",
      "type": "application/json; name=menu_category_list"
    }
  },
  "count": 2,
  "limit": 2
}

【问题讨论】:

    标签: json swift codable hal-json


    【解决方案1】:

    您可以让 _embedded 键每次都接受通用的 Codable 结构而不是特定类型。

    struct Category<T: Codable>: Codable {
        var embedded: T?
        var links: CategoryLinksClass?
        var count, limit: Int?
    
        enum CodingKeys: String, CodingKey {
            case embedded = "_embedded"
            case links = "_links"
            case count, limit
        }
    }
    

    并创建为"_embedded" 键返回的不同模型。

    struct Embedded: Codable { ... }
    
    struct Menu: Codable { ... }
    

    然后像这样提供解码时的模型类型:

    do { let decoded = JSONDecoder().decode(Category<Embedded>.self, from: data) 
    } catch { print(error) }
    

    【讨论】:

    • 谢谢你今晚试试!
    猜你喜欢
    • 2015-05-17
    • 2015-08-27
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多