结构可编码。无法编码嵌套的json

时间:2018-09-27 10:48:38

标签: json swift codable

我已经检查了许多关于SO的其他问题,即使它们看起来很相似,也没有解决我目前在实施中遇到的需求和挑战。到目前为止,我所看到的问题和答案更多地涉及了《可解码》。但是我目前遇到了可编码的问题

我有嵌套的JSON结构,如下所示。我能够成功解码并传递json

"data": {
    "id": "XXXXXXXXXX",
    "tKey": "XXXXXXXX",
    "tID": "XXXXX",
    "type": "XXXXX",
    "hasEvent": true,
    "location": "XXXXXXXXXXXXX",
    "cover": {
        "name": "XXXXXXX",
        "parentId": "XXXXXXX",
        "parentName": "XXXXXX",
        "pop": {
            "logo": "*********",
        }
},
"men": [
  {
    "id": "XXXXXXXXXXX",
    "title": "XXXXXXX"
  },
  {
    "id": "XXXXXX",
    "title": "XXXXXX"
  },
  {
    "id": "XXXXXX",
    "title": "XXXXXX"
  }
],
"homes": [],
"homeTypes": [{
    "id": "XXXXXX",
    "subMenuId": "XXXXXXXX",
    "type": "Flat",
    "data": {
      "latitude": 29.767,
      "longitude": 0,0000,
    },
    "datas": null,
    "component": null
  },
  {
    "id": "XXXXXXXXX",
    "subMenuId": "XXXXX",
    "type": "Bubgalow",
    "data": {
      "id": "XXXXXXXXX,
      "title": "XXXXXXXXX",
      "summary": "Hello;",
  }
 }
]
}

可解码的工作正常,因为我能够从服务器传递响应并填充tableview。但是,在尝试使用可编码协议进行编码时似乎无效。请在下面找到我尝试过的代码片段:

Struct DataObject: Codable {
var id: String = ""
var location: String = ""
var tKey: String = ""
var tId: String = ""
var cover: cover!
var homeTypes: [Type] = [TYpe]()

var flat = Flat()
var bungalow = Bungalow()

var flat = Flat()
var bungalow = Bungalow()

enum CodingKeys: String, CodingKey {
    case id = "id"
    case type = "type"
    case location = "location"
    case tKey = "tKey"
    case tId = "tId"
    case cover = "cover"
    case homeTypes = "homeTypes"
}

enum ComponentCodingKeys: String, CodingKey {
    case id = "id"
    case type = "type"
    case component = "component"
}


required init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    id = try container.decode(String.self, forKey: .id)
    if container.contains(.tKey) {
        tKey = try container.decodeIfPresent(String.self, forKey: .tKey) ?? ""
    }
    if container.contains(.tId) {
        tId = try container.decodeIfPresent(String.self, forKey: .tId) ?? ""
    }
    let typeRawValue = try container.decode(String.self, forKey: .type)
    if let tempType = SomeType(rawValue: typeRawValue) {
        type = tempType
    }
    if container.contains(.location) {
        location = try container.decodeIfPresent(String.self, forKey: .location) ?? ""
    }
    cover = try container.decode(Cover.self, forKey: .cover)
    homeTypes = try container.decode([HomeTypes].self, forKey: .component)
     for homeType in homeTypes {

          switch homeType.type {
           case .flat:
                flat = homeType.flat
          }
          case .bungalow:
                bungalow = homeType.bungalow
     }

    }

    func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(id, forKey: .id)
    try container.encode(location, forKey: .location)
    try container.encode(tKey, forKey: .tKey)
    try container.encode(tId, forKey: .tId)
    try container.encode(cover, forKey: .cover)
    //let rawValueType = try container.encode(type, forKey: .type)

    var homes = container.nestedUnkeyedContainer(forKey: .component)
    try homeTypes.forEach {
        try homes.encode($0)
     }
    }
   }
  }

1 个答案:

答案 0 :(得分:0)

您的代码有几个问题。首先整理这些内容,然后您可能会更清楚地知道发生了什么

  • 使用Make a Choice: 1.Stone 2.Paper 3.Scissor Player 1 plays 1 Player 2 plays 3 Traceback (most recent call last): File "G:/Study/Python/StonePaperScissor.py", line 7, in <module> if player1 is 1 and player2 is 3 or player1 is 2 and player2 is 1 or player1 is 3 and player2 is 2: NameError: name 'player1' is not defined Process finished with exit code 1 而不是let。如果不存在值,请使用var而不是空字符串。

    nil
  • 如果您的struct DataObject: Codable { let id: String let location: String? let tKey: String? let tId: String? let cover: Cover let homeTypes: [Type] } 与json键匹配,则无需为它们提供字符串值。所以……

    CodingKeys
  • 如果您使用的是enum CodingKeys: CodingKey { case id, type, location, tKey, tId, cover, homeTypes }

    ,则无需测试密钥是否存在
    decodeIfPresent
  • 先创建tKey = try container.decodeIfPresent(String.self, forKey: .tKey) ,然后

    Type: Codable