简体   繁体   中英

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 . Since my table view was still empty, I added a breakpoint on the JSONDecoder.decode line to see what is going on. I get this error, even though I made sure that my naming convention is the same in both my structs and JSON file. 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. 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:

{
    "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.

Maybe try to modify your json like this:

{
    "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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM