- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试解析来自 Create Team 的响应时调用 Github 的 REST api,JSONDecoder
在解析存储库的许多蛇形键时失败。当通过JSONSerialization
解码时,它能够毫无问题地找到所有 key 。
例如Xcode 11.0(11A420a)在Playground中运行时,用JSONDecoder
解码时解码失败。
import Foundation
let jsonData = """
{
"id": 12345,
"name": "swift",
"ssh_url": "git@github.com:apple/swift.git"
}
""".data(using: .utf8)!
struct ExampleModel: Codable {
let id: Int
let name: String
let sshURL: String
}
let jsonObject = try! JSONSerialization.jsonObject(with: jsonData, options: []) as! [String: Any]
print("JSONSerialization:", jsonObject["id"]!, jsonObject["name"]!, jsonObject["ssh_url"]!)
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let decodedObject = try! decoder.decode(ExampleModel.self, from: jsonData) // Fails here
print("JSONDecoder:", decodedObject.id, decodedObject.name, decodedObject.sshURL)
// Output:
//
// JSONSerialization: 12345 swift git@github.com:apple/swift.git
// Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "sshURL", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"sshURL\", intValue: nil) (\"sshURL\"), converted to ssh_url.", underlyingError: nil)): file MyPlayground.playground, line 22
我应该做些什么来解析这个值?
快速版本:
Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7)
Target: x86_64-apple-darwin19.0.0
最佳答案
尝试将 sshURL
更改为 sshUrl
。 keyDecodingStartegy
会将 sshURL
转换为 ssh_URL
,这与您的 key 不匹配。 sshUrl
将转换为 ssh_url
,它将与您的 key 匹配。
关于ios - JSONDecoder 找不到存在的 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58223590/
我有一个 .json 文件,其中有一些 map ,还有一些带有位置的数组。我想将其解码为一个对象,但我不断收到错误消息,指出我的 .json 格式不正确。这可能很愚蠢,但如果有人可以提供帮助,我们将不
上下文 我正在使用 Firebase Database REST API 和 JSONDecoder/JSONEncoder。到目前为止,它一直运行良好。然而,对于 removing data,预期返
我的 iOS 应用程序在解码服务器响应时发现了一些内存泄漏。这些内存泄漏不会一直发生。我正在使用以下代码来解码响应。这些代码位于通用结构中。 T.self 也是一个结构而不是一个类。 struct R
如何为 Int、Bool 等 primitive 类型自定义 JSONDecoder 的行为? 问题是: 类型不能依赖后端。例如: bool 可以是真/假或“真”/“假”( bool 可以用双引号引起
我正在使用一个将每个字段作为字符串返回的 JSON api。然而,其中一些字符串值实际上表示时间戳以及经度和纬度(作为单独的字段)。 如果我将这些值分别指定为 Date 和 Double,它会 swi
我正在使用新的 Swift 4 Codable 接口(interface)从网络服务中简单地获取 JSON 数据。我试图实现一个通用类型方法来处理解码(因此我不需要自定义方法)但我一直收到错误。这是代
我正在使用 JSONDecoder 从具有嵌套字典的 JSON 文件中解码。它无法从 json 数据解码到我的自定义模型。 这是我在代码中尝试过的。 JSONDecoder 看起来像这样: let n
我正在尝试使用 Alamofire 和 SwiftyJSON 快速将一组 JSON 字典加载到一个对象中,但是当下载数据时,我得到错误: keyNotFound(CodingKeys(stringVa
我正在使用 JSONDecoder 将 JSON 解码为 MyModel 数组: myModelArray : [MyModel] = try JSONDecoder().decode([MyMode
已阅读 Joel on Encoding像一个好 child 一样,我发现自己对 Foundation 的 JSONDecoder 的工作方式感到困惑,它的 init 或 decode 方法都不采用编
这是一个模型设计相关的问题,与 swift 上的 JSONDecoder 可编码功能有关。 我有以下 JSON: "author": { "name": "abc", "emailAd
我正在尝试使用 MLFlow CLI 运行 MLFlow 项目,但按照教程进行操作会导致错误。对于我尝试从 CLI 运行的任何项目,我都会收到以下错误 Traceback (most recent c
给定这个结构: public struct Error: Codable { public let code: String public let message: String
我有许多模型,取决于端点,或者以不同的方式序列化。我的第一次尝试是 init(from decoder: Decoder)充满嵌套try catch block 。我认为更好的解决方案是扩展 JSON
我的json中的日期参数可以为空(“”)。在 JSONDecoder 中如何处理? 自定义的dateDecodingStrategy只能处理不同的格式,不能处理空值。(格式化程序返回的日期不能是可选的
我是 Swift JSON 新手,所以我正在 Swift Playgrounds 中练习。我非常确定这将被视为解码嵌套的 JSON 键。但就像我说的,我是新人,我并不熟悉所有技术术语。 无论如何,我认
我有一个名为contracters_system的表,我想删除contracter_id中的特定值,我该怎么做? $plansarray = Array ( [0] => 4 [1] => 10 [2
我试图将 getstream 集成到我的应用程序中,我需要一些帮助来解决我遇到的 jsonDecode 错误。 以下是我采取的步骤:1.手动创建一个名为allevents的新feedgroup 2.我
我使用了 Json4Swift 网站。 根模型: struct Json4Swift_Base : Codable { let trends : [Trends]? let as_of
我正在尝试使用通用嵌套对象从 json 对象解码,为此我想在解码时动态传递类的类型。 例如,我的类是扩展了 ObjectModel(和 :Codable)的 EContactModel 和 ENoti
我是一名优秀的程序员,十分优秀!