- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试借助 codable 解码 JSON - https://pastebin.com/Xfjj2XiP
但是,当我这样做时出现了这个错误。
typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "txt_forecast", intValue: nil)], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil))
这是我使用的代码:
struct container: Decodable {
var days: [forecastDay]
//Coding keys
enum CodingKeys: String, CodingKey {
case forecast = "forecast"
case txt_forecast = "txt_forecast"
case forecastday = "forecastday"
}
// Decoding
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let forecast = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .forecast)
let txt_forecast = try forecast.nestedContainer(keyedBy: CodingKeys.self, forKey: .txt_forecast)
let forecastdays = try txt_forecast.nestedContainer(keyedBy: CodingKeys.self, forKey: .forecastday)
let forecastdaysData = try forecastdays.decode(String.self, forKey: .forecastday)
days = try JSONDecoder().decode([forecastDay].self, from: forecastdaysData.data(using: .utf8)!)
print(days)
}
}
struct forecastDay: Decodable {
var period: Int?
var icon: String?
var title: String?
var fcttext: String?
//Coding keys
enum CodingKeys: String, CodingKey {
case period = "period"
case icon = "icon"
case title = "title"
case fcttext = "fcttext"
}
// Decoding
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
period = try container.decode(Int.self, forKey: .period)
icon = try container.decode(String.self, forKey: .icon)
title = try container.decode(String.self, forKey: .title)
fcttext = try container.decode(String.self, forKey: .fcttext)
}
}
最佳答案
forecastDay
中的变量名称与 JSON 中的键没有区别,init(from decoder:)
也没有任何自定义,因此预测日的结构期间可以简化。
struct ForecastDayPeriod: Decodable {
let period: Int
let icon: String
let title: String
let fcttext: String
}
现在最好为 JSON 中的每个级别使用带有键的枚举。此外,init(from decoder:)
不应创建新的 JSONDecoder
。
struct ForecastDay: Decodable {
let periods: [ForecastDayPeriod]
enum CodingKeys: String, CodingKey {
case forecast
}
enum ForecastKeys: String, CodingKey {
case txtForecast = "txt_forecast"
}
enum TxtForecastKeys: String, CodingKey {
case forecastday
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
let forecast = try values.nestedContainer(keyedBy: ForecastKeys.self,
forKey: .forecast)
let txtForecast = try forecast.nestedContainer(keyedBy: TxtForecastKeys.self,
forKey: .txtForecast)
periods = try txtForecast.decode([ForecastDayPeriod].self,
forKey: .forecastday)
}
}
现在应该可以从 pastebin 示例中解码 JSON。
do {
let jsonData: Data = ...
let forecastDay = try JSONDecoder().decode(ForecastDay.self, from: jsonData)
} catch {
print("Error: \(error)")
}
关于swift - Codable 错误 - 预期解码 Dictionary<String, Any> 但发现了一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50439824/
给定这个类: 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类型,可选
我是一名优秀的程序员,十分优秀!