响应结构不喜欢 CodingKeys [英] Response struct does not like CodingKeys

查看:30
本文介绍了响应结构不喜欢 CodingKeys的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个结构来解码来自服务器的 JSON:

struct AdminResponse: Codable {让状态:Int?让超级用户:[超级用户]?让消息:字符串?}struct SuperUser: Codable {让名称:字符串?让id:字符串?}

这完美地工作.我能够解码 JSON.

但我不喜欢第一个大写的这些属性,那么我喜欢

struct AdminResponse: Codable {让状态:Int?让超级用户:[超级用户]?让消息:字符串?枚举编码键:字符串,编码键{案例状态=状态"case superUsers = "SuperUsers";案例消息=消息"}}struct SuperUser: Codable {让名称:字符串?让id:字符串?枚举编码键:字符串,编码键{案例名称=名称"case id =id"}}

<块引用>

错误:类型 AdminResponse 不符合可解码协议.

为什么???

解决方案

来自文档 编码和解码自定义类型:

<块引用>

Codable 类型可以声明一个名为的特殊嵌套枚举符合 CodingKey 协议的 CodingKeys.当这列举存在,其案例作为权威列表可编码类型的实例时必须包含的属性编码或解码.枚举案例的名称应该匹配您为类型中的相应属性指定的名称.

换句话说:

struct CodableStruct: Codable {让 myPropertyName: SomeCodableType枚举编码键:字符串,编码键{case myPropertyName = "WhateverIsMatchingInRealityTheJSON";}}

并且myPropertyName 必须是CodableStruct 的var 的名称和CodingKeys<的case 的名称/代码>.

在您的情况下,由于建议以小写字母开头命名 var,我会这样做:

struct AdminResponse: Codable {让状态:Int?让超级用户:[超级用户]?让消息:字符串?枚举编码键:字符串,编码键{案例状态=状态"case superUsers = "SuperUsers";案例消息=消息"}}

I have this struct used to decode a JSON coming from the server:

struct AdminResponse: Codable {
  let Status: Int?
  let SuperUsers: [SuperUser]?
  let Message: String?
}

struct SuperUser: Codable {
  let name:String?
  let id:String?
}

This works perfectly. I am able to decode the JSON.

But I don't like these properties with the first uppercase, then I do

struct AdminResponse: Codable {
  let Status: Int?
  let SuperUsers: [SuperUser]?
  let Message: String?
  
  enum CodingKeys: String, CodingKey {
    case status = "Status"
    case superUsers = "SuperUsers"
    case message = "Message"
  }
}

struct SuperUser: Codable {
  let name:String?
  let id:String?

  enum CodingKeys: String, CodingKey {
    case name = "name"
    case id = "id"
  }
}

Error: Type AdminResponse does not conforme to protocol Decodable.

Why???

解决方案

From the documentation Encoding and Decoding Custom Types:

Codable types can declare a special nested enumeration named CodingKeys that conforms to the CodingKey protocol. When this enumeration is present, its cases serve as the authoritative list of properties that must be included when instances of a codable type are encoded or decoded. The names of the enumeration cases should match the names you've given to the corresponding properties in your type.

In other words:

struct CodableStruct: Codable {
  let myPropertyName: SomeCodableType
  
  enum CodingKeys: String, CodingKey {
    case myPropertyName = "WhateverIsMatchingInRealityTheJSON"
  }
}

And it's mandatory that myPropertyName be the name of the var of CodableStruct AND the name of the case of the CodingKeys.

In your case, since it's recommended to name the var starting with a lowercase, I'll go that way:

struct AdminResponse: Codable {
  let status: Int?
  let superUsers: [SuperUser]?
  let message: String?
  
  enum CodingKeys: String, CodingKey {
    case status = "Status"
    case superUsers = "SuperUsers"
    case message = "Message"
  }
}

这篇关于响应结构不喜欢 CodingKeys的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆