简体   繁体   中英

How do I fix this Swift "decoding could not find key CodingKeys" error?

**I have this JSON: **

{
    "results": [
        {
            "dimensions": [],
            "metrics": [
                385,
                54,
                18263,
                120
            ]
        }
    ],
    "previous": null,
    "next": "https://app.tester.com/api/v1/projects/dev-js/tester/query?page=2",
    "page": 1,
    "size": 500,
}

**That I need to decode. Currently my STRUT looks like this: **

struct APIRootEA: Codable {
        let results: String?
        let dimensions: String?
        let metrics: String?
        let EAResults: [ResultsEA]
    }
    
    struct ResultsEA: Codable {
        let eaRevenue: Int
        let eaTransactions: Int
        let eaVisits: Int
        let eaActiveUrls: Int
        let previous: String?
        let next: String?
        let page, size: Int
           
        enum CodingKeys: String, CodingKey {
            case eaRevenue
            case eaTransactions
            case eaVisits
            case eaActiveUrls
            case previous
            case next
            case page
            case size
           }
       }

But i'm getting the error:

could not find key CodingKeys(stringValue: "eaRevenue", intValue: nil) in JSON: No value associated with key CodingKeys(stringValue: "eaRevenue", intValue: nil) ("eaRevenue").

The raw data returned from the URLRequest is:

Optional("{"results":[{"dimensions":[],"metrics":[82,289198.5079709999,64782,5218]}],"previous":null,"next":"https://api.tester.com/v1/projects/tester-solutions/tester-project/query?page=2","page":1,"size":500}")

I don't understand why. Usual proviso here, i'm very much a noob with Swift and this is my first attempt so i'm reaching out for help.

I'd appreciate any advice.

I've tried manipulating the STRUT with no success.

When creating a struct that decodes from JSON you should start with at least something that looks like the JSON it is being decoded from.

For instance, in your JSON you have...

"metrics": [12, 45, 643, 58],

ie there is a key metrics and it points to an array of integers.

So, to decode this "for free" (ie without writing a custom decoder) you should have a struct like...

struct MyStruct: Decodable {
  let metrics: [Int]
}

If you set metrics to anything else it just won't work. (without a custom decoder)

Also, your coding keys is a way to tell Swift what the keys are inside the JSON. Your coding keys contains a key eaRevenue but there is no such key in the JSON. This is why you're getting the error you're receiving.

It looks like there is some quite complex logic that you are doing to map from the JSON to the struct. It might be worth starting with something more straight forward and slowly building up to a custom decoder for this behaviour.

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