如何使用Decodable协议将此JSON转换为Swift结构?

时间:2017-06-16 16:59:32

标签: json swift codable

注意:我已经看过这个问题了 - > form: option但它没有解释如何编码/解码枚举

这是我想要的结构:

struct MyStruct: Decodable {
    let count: PostType
}

enum PostType: Decodable {
    case fast(value: Int, value2: Int)
    case slow(string: String, string2: String)
}

现在我知道我希望我的结构看起来如何,问题是:

  1. 我不知道init枚举内PostType函数应该是什么样的。
  2. 我使用下面的代码来帮助我快速构建JSON。

        let jsonData = """
        {
           "count": {
              "fast" :
               {
                   "value": 4,
                   "value2": 5
               }
           }
        }
        """.data(using: .utf8)!
    
        // Decoding
        do {
            let decoder = JSONDecoder()
            let response = try decoder.decode(MyStruct.self, from: jsonData)
            print(response)
        } catch {
            print(error)
        }
    

    任何人都可以帮我吗?

    Edit1 我的JSON看起来像这样

       {
            "count": {
               "fast" :
                {
                    "value": 4,
                    "value2": 5
                }
            }
        }
    

    init中的PostType enum应该是什么样的?

1 个答案:

答案 0 :(得分:5)

由于具有关联类型的enum与任何JSON类型都不匹配,因此您需要更多的手工操作并编写自定义映射。

以下代码涵盖三个选项。

  • 以案例为键并包含参数标签为键的词典的词典
  • 将每个关联值编码/解码为单独的键/值对
  • 使用任意键对数组中的所有关联值进行编码/解码。

首先,枚举不得与Codable

一致
enum PostType {
    case fast(value: Int, value2: Int)
    case middle(bool: Bool)
    case slow(string: String, string2: String)
}

案例fast使用数组,middle子字典,slow个单独的键/值对。

然后声明MyStruct结构,采用Codable并声明type

struct MyStruct : Codable {
    var type : PostType

此解决方案需要自定义键

  enum CodingKeys: String, CodingKey {
      case value, string, string2, middle
  }

案例encode方法switch并创建适当的类型

  func encode(to encoder: Encoder) throws {
     var container = encoder.container(keyedBy: CodingKeys.self)
      switch type {
      case .fast(let value, let value2) :
          try container.encode([value, value2], forKey: .value)

      case .slow(let string, let string2) :
          try container.encode(string, forKey: .string)
          try container.encode(string2, forKey: .string2)

      case .middle(let bool):
          try container.encode(["bool" : bool], forKey: .middle)
      }
  }

decode方法中,您可以通过传递的密钥区分案例,确保它们是唯一的。

  init(from decoder: Decoder) throws {
      let values = try decoder.container(keyedBy: CodingKeys.self)
      let allKeys = values.allKeys
      if allKeys.contains(.middle) {
          let value = try values.decode([String:Bool].self, forKey: .middle)
          type = PostType.middle(bool: value["bool"]!)
      } else if allKeys.contains(.value) {
          let value = try values.decode([Int].self, forKey: .value)
          type = PostType.fast(value: value[0], value2: value[1])
      } else {
          let string = try values.decode(String.self, forKey: .string)
          let string2 = try values.decode(String.self, forKey: .string2)
          type = PostType.slow(string: string, string2: string2)
      }
   }
}

虽然有些键是硬编码的,但第一个选项似乎是最合适的。

最后一个使用它的例子:

let jsonString = "[{\"value\": [2, 6]}, {\"string\" : \"foo\", \"string2\" : \"bar\"}, {\"middle\" : {\"bool\" : true}}]"

let jsonData = jsonString.data(using: .utf8)!

do {
    let decoded = try JSONDecoder().decode([MyStruct].self, from: jsonData)
    print("decoded:", decoded)

    let newEncoded = try JSONEncoder().encode(decoded)
    print("re-encoded:", String(data: newEncoded, encoding: .utf8)!)

} catch {
    print(error)
}
相关问题