gpt4 book ai didi

swift - 使用 Codable 时,如何在枚举 CodingKeys 中指定要解码的多种类型?

转载 作者:行者123 更新时间:2023-11-30 10:44:11 28 4
gpt4 key购买 nike

我有一个 struct 类型,其中列出了必须从 JSON 解码的不同类型,并且我找不到任何有关如何在我的 enum 中指定多个类型的信息,称为编码键。在最里面的字典中,有时一个键:值对将是 [String:String],另一个键:值对将是 [String:Int]

我尝试仅指定 StringCodingKey,如下面的代码片段所示,但 Xcode 报告了 2 条错误消息。

struct JSONSiteData : Codable {
let destinationAddresses : [String]
let originAddresses : [String]
let rows : [ [String : [[String:[[String: [String:Any]]]]]] ]
let status : String
}

enum CodingKeys : String, CodingKey {
case destinationAddresses = "destination_addresses"
case originAddresses = "origin_addresses"
case rows
case status
}

我从 Xcode 收到以下错误消息;

Type 'JSONSiteData' does not conform to protocol 'Decodable'
Type 'JSONSiteData' does not conform to protocol 'Encodable'

这是我的 JSON;

{
"destination_addresses": [
"1 Dunwell Ln, Bolam, Darlington DL2 2UW, UK",
"Unnamed Road, Newton Aycliffe DL5 6QZ, UK",
"Preston Manor Farm, Preston le Skerne, Newton Aycliffe DL5 6JH, United Kingdom",
"6 Middridge Farms, Middridge, Newton Aycliffe DL5 7JQ, UK",
"1 The Gardens, Hunwick, Crook DL15 0XW, UK"
],
"origin_addresses": [
"42 Drovers Way, Dunstable LU6 1AW, UK"
],
"rows": [
{
"elements": [
{
"distance": {
"text": "220 mi",
"value": 353731
},
"duration": {
"text": "3 hours 45 mins",
"value": 13475
},
"status": "OK"
},
{
"distance": {
"text": "222 mi",
"value": 356696
},
"duration": {
"text": "3 hours 45 mins",
"value": 13471
},
"status": "OK"
},
{
"distance": {
"text": "222 mi",
"value": 358053
},
"duration": {
"text": "3 hours 46 mins",
"value": 13545
},
"status": "OK"
},
{
"distance": {
"text": "225 mi",
"value": 361421
},
"duration": {
"text": "3 hours 49 mins",
"value": 13768
},
"status": "OK"
},
{
"distance": {
"text": "229 mi",
"value": 369280
},
"duration": {
"text": "3 hours 57 mins",
"value": 14238
},
"status": "OK"
}
]
}
],
"status": "OK"
}

最佳答案

你可以试试这个,

// To parse the JSON, add this file to your project and do:
//
// let jsonSiteData = try? newJSONDecoder().decode(JSONSiteData.self, from: jsonData)

import Foundation

struct JSONSiteData: Codable {
let destinationAddresses, originAddresses: [String]
let rows: [Row]
let status: String

enum CodingKeys: String, CodingKey {
case destinationAddresses = "destination_addresses"
case originAddresses = "origin_addresses"
case rows, status
}
}

struct Row: Codable {
let elements: [Element]
}

struct Element: Codable {
let distance, duration: Distance
let status: String
}

struct Distance: Codable {
let text: String
let value: Int
}

请参阅此链接以从 JSON 字符串生成模型。 https://app.quicktype.io/

关于swift - 使用 Codable 时,如何在枚举 CodingKeys 中指定要解码的多种类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56134309/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com