- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 struct
类型,其中列出了必须从 JSON 解码的不同类型,并且我找不到任何有关如何在我的 enum
中指定多个类型的信息,称为编码键
。在最里面的字典中,有时一个键:值对将是 [String:String]
,另一个键:值对将是 [String:Int]
我尝试仅指定 String
和 CodingKey
,如下面的代码片段所示,但 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/
给定这个类: class MyClass: Codable { var variable : Codable? = nil } 我得到错误: Type 'MyClass' does not c
鉴于 Array 符合 Codable 我假设 Codable 的数组,即 [Codable] 应该肯定可转换为 Codable。 我已经用 Decodable 部分做了一个简单的例子。并且只是为了验
我希望创建一个类来存储 Date 和任何符合 Codable 协议(protocol)的对象。我希望此类也符合 Codable 协议(protocol)本身。 我可以按如下方式为一个对象执行此操作:
ClassA 符合 Cadable 并具有一系列属性。其中之一是不符合 Codable 的已经存在的非常复杂的 ClassB 的属性。我可以手动解码 Codable 类的非 Codable 属性吗?
当遵循 Codable 协议(protocol)时,我不能轻易跳过非 Codable 类的可选属性 在 Ride 结构中,我们希望跳过 driver 属性的编码 和解码,并保留它 nil 解码时:
假设我有一些 JSON,如下所示: { "some-random-key": { "timestamp": 1234123423
很多人告诉我,codable 比使用 swiftyjson 好得多,所以我正在尝试一下。我想将此 JSON 的返回值 https://api.gdax.com/products/BTC-USD/boo
我有一个 Codable 类型,比方说 Car ,它被定义为: struct Car: Codable { let age: Int let color: String } 我可以很好
我正在尝试实现类似于 Swift 如何在实现 Codable 的类中定义的枚举上使用 CodableKeys 协议(protocol)集的方式。在我的例子中,类是 CommandHandler,枚举是
我正在尝试将“Object Mapper”转换为“Codable”。我来自服务的响应包括 NSArray,其中包含自定义对象。我需要在 Codable 类中使用 NSArray 和 NSDiction
使用 Codable,我可以创建以下扩展 extension Decodable { public static func decode(data: Data, decoder: JSONDe
我有一个 API,它以前缀表示法接收查询。例如(+ 1 2)这可以递归地完成,例如(+ 1 (- 1 2)) 准确地说,所需的 json 如下所示: { "query":[ {
struct Data: Codable { let name: String? let dataArray: [User] = [User]() } dataArray 是可选的,所以我想用
这个问题在这里已经有了答案: check the json response is array or int or string for a key? (7 个答案) 关闭 3 年前。
我正在使用通用编码器来传递网络响应。我创建了以下函数来解码数据并返回结果: 这是该函数的一部分: guard let jsonDataUnwrapped = jsonData,
我有一个问题,我想我已经掌握了每个问题的基础知识,但不太确定如何将它们结合起来。 在 View Controller 中我有一个基本的 var 设置: var shipments = [Shipmen
我有一个(恼人的)情况,我的后端返回一个像这样的对象: { "user": { "name": [ "John" ], "fam
这个问题在这里已经有了答案: Swift 4 JSON Decodable with multidimensional and multitype array (5 个答案) 关闭 5 年前。 AP
我的 JSON 响应如下: { "data": [ { "unknown-key-c3e7f0": { "date_ti
我正在尝试为一个对象采用 Codable 协议(protocol),该对象必须从我的 Web 服务返回的 JSON 实例化以响应其中一个 API 调用。 其中一个属性是enumeration类型,可选
我是一名优秀的程序员,十分优秀!