-1

I've just come across a problem in Swift that I haven't found a solution for: When you decode JSON data (from an API) in Swift, you have to decode it using JSONDecoder, but this forces me to name my variable name exactly like the JSON property that I want to retrieve - the problem is, that the JSON-property's name has a dash in it ("saturated-fat_100g"). Of course, I can't call my variable like that due to the dash. Is there any workaround to this issue?

I want to retrieve the JSON property called saturated-fat_100g

mirco03
  • 27
  • 3

1 Answers1

4
struct test: Codable {
    let testtest: String
    let instructions: [String]

    enum CodingKeys: String, CodingKey {
        case testtest = "test-test"
        case instructions
    }
}

Here is an example. you use coding keys to identify the "-" dash.

Julian Silvestri
  • 1,970
  • 1
  • 15
  • 33