在Swift中使用JSONDecoder时,我不断收到“与键无关的值”错误。有人可以解释一下这里出了什么问题吗?

时间:2019-05-30 12:48:22

标签: ios json swift codable

我正在尝试从本地JSON文件中读取数据,并在tableView中填充要从Decoder中检索的单元格。由于我的表视图仍然为空,因此在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\")."

1 个答案:

答案 0 :(得分:0)

我认为问题在于解码器希望使用项Section的数组,但是您的json具有字典数组,其中包含键“ section”和项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": ""
                }
  ]}]
}

更新

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