[英]I keep getting the “No value associated with key” error when using the JSONDecoder in Swift. Can someone please explain what is going wrong here?
I am trying to read from a local JSON
file and populate my tableView with cells to be retrieved from the Decoder
. 我试图从本地
JSON
文件读取并填充我的tableView与要从Decoder
检索的单元格。 Since my table view was still empty, I added a breakpoint on the JSONDecoder.decode
line to see what is going on. 由于我的表视图仍然是空的,我在
JSONDecoder.decode
行上添加了一个断点,以查看发生了什么。 I get this error, even though I made sure that my naming convention is the same in both my structs and JSON
file. 我收到此错误,即使我确保我的命名约定在我的结构和
JSON
文件中是相同的。 Is there something I am missing here. 这里有什么我想念的东西。
Since my naming convention was consistent across the files, at first I did not try add CodingKeys enum
inside my structs. 由于我的命名约定在文件中是一致的,起初我没有尝试在我的结构中添加
CodingKeys enum
。 After a few failed runs I added that in but it feels kind of obsolete. 经过一些失败的运行后,我加入了它,但它感觉有点过时了。
Where I run the decoder: 我在哪里运行解码器:
let fileName = "settings"
...
if let url = Bundle.main.url(forResource: fileName, withExtension: "json") {
do {
let data = try Data(contentsOf: url)
let list = try JSONDecoder().decode(SettingsPayload.self, from: data)
completion(list.sections)
} catch {
completion(nil)
}
}
fileprivate struct SettingsPayload: Decodable {
let sections: [Section]
}
My structs that will be used to store the data when retrieved: 检索时将用于存储数据的结构:
struct Item: Decodable {
let title: String
let type: String
let url: String
private enum CodingKeys: String, CodingKey {
case title = "title"
case type = "type"
case url = "url"
}
}
struct Section: Decodable {
let title: String
let items: [Item]
private enum CodingKeys: String, CodingKey {
case title = "title"
case items = "items"
}
}
and my .json
file: 和我的
.json
文件:
{
"sections": [{
"section": {
"title": "Main Settings",
"items": [{
"item": {
"title": "Notifications",
"type": "notification",
"url": ""
},
"item": {
"title": "Log Out",
"type": "",
"url": ""
}
}]
},
"section": {
"title": "Feedback",
"items": [{
"item": {
"title": "Contact Us",
"type": "email",
"url": ""
},
"item": {
"title": "Rate on App Store",
"type": "webView",
"url": "https://www.apple.com/uk/ios/app-store/"
}
}]
},
"section": {
"title": "About",
"items": [{
"item": {
"title": "Terms of Service",
"type": "webView",
"url": ""
},
"item": {
"title": "Privacy Policy",
"type": "webView",
"url": "https://www.apple.com/uk/ios/app-store/"
},
"item": {
"title": "Version Info",
"type": "webView",
"url": ""
}
}]
}
}]
}
This is the error message I get: 这是我收到的错误消息:
- debugDescription : "No value associated with key CodingKeys(stringValue: \"title\", intValue: nil) (\"title\")."
I think the problem is that the decoder expects an Array of the item Section, but your json has an Array of dictionaries with a key "section" and an item Section in there. 我认为问题是解码器需要一个项目部分的数组,但你的json有一个字典数组,其中包含一个键“section”和一个项目Section。
Maybe try to modify your json like this: 也许尝试像这样修改你的json:
{
"sections": [{
"title": "Main Settings",
"items": [ {
"title": "Notifications",
"type": "notification",
"url": ""
}, {
"title": "Log Out",
"type": "",
"url": ""
}]}, {
"title": "Feedback",
"items": [{
"title": "Contact Us",
"type": "email",
"url": ""
},{
"title": "Rate on App Store",
"type": "webView",
"url": "https://www.apple.com/uk/ios/app-store/"
}]}, {
"title": "About",
"items": [{
"title": "Terms of Service",
"type": "webView",
"url": ""
}, {
"title": "Privacy Policy",
"type": "webView",
"url": "https://www.apple.com/uk/ios/app-store/"
}, {
"title": "Version Info",
"type": "webView",
"url": ""
}
]}]
}
Update 更新
JSONDecoder doesn't look for the name of your decodable struct in the JSON, it only looks for the name of the properties. JSONDecoder不会在JSON中查找可解码结构的名称,它只查找属性的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.