在 Swift 中使用 JSONDecoder 时,我不断收到 "No value associated with key" 错误。有人可以解释这里出了什么问题吗?

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?

我正在尝试从本地 JSON 文件中读取并使用要从 Decoder 检索的单元格填充我的 table 视图。由于我的 table 视图仍然是空的,我在 JSONDecoder.decode 行上添加了一个断点以查看发生了什么。我得到这个错误,即使我确保我的命名约定在我的结构和 JSON 文件中是相同的。我在这里遗漏了什么吗?

由于我的命名约定在文件中是一致的,起初我没有尝试在我的结构中添加 CodingKeys enum。在几次失败 运行 之后,我添加了它,但感觉有点过时了。

我在哪里 运行 解码器:

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]
}

检索时将用于存储数据的我的结构:

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"
    }
}

和我的 .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": ""
                }
            }]
        }
    }]
}

这是我收到的错误消息:

     - debugDescription : "No value associated with key CodingKeys(stringValue: \"title\", intValue: nil) (\"title\")."

我认为问题在于解码器需要项目部分的数组,但你的 json 有一个字典数组,其中有一个键 "section" 和一个项目部分。

也许可以尝试像这样修改您的 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": ""
                }
  ]}]
}

更新

JSON解码器不会在 JSON 中查找可解码结构的名称,它只会查找属性的名称。