0

I am making app and I am taking data from http but for testing copied and pasted example into file and I don't know how to create model when one of values is changing every call, but its not in an array. Here is json file:

{
    "updated":1666610417642,
    "status":"OK",
    "data":{
        "bygarasjen":
        {
            "Name":"ByGarasjen, Bergen",
            "NumFreeSpaces":207,
            "NumAvailableChargepoints":17,
            "NumOccupiedChargepoints":37,
            "CurrentPrice":34,
            "CurrentPriceElectric":17,
            "MaxPricePer24Hours":150,
            "StatusMessage":""
        },
        "klostergarasjen":
            {
                "Name":"KlosterGarasjen, Bergen",
                "NumFreeSpaces":295,
                "NumAvailableChargepoints":1,
                "NumOccupiedChargepoints":23,
                "CurrentPrice":42,
                "CurrentPriceElectric":21,
                "MaxPricePer24Hours":200,
                "StatusMessage":""
            },
        "nordnes":
            {
                "Name":"NordnesGarasjen, Bergen","NumFreeSpaces":"Midlertidig utilgjengelig","NumAvailableChargepoints":1,"NumOccupiedChargepoints":8,"CurrentPrice":30,"CurrentPriceElectric":15,"MaxPricePer24Hours":0,"StatusMessage":""
            },
            "solheimsviken":{
                "Name":"SolheimsGarasjen, Bergen","NumFreeSpaces":"Midlertidig utilgjengelig","NumAvailableChargepoints":5,"NumOccupiedChargepoints":13,"CurrentPrice":40,"CurrentPriceElectric":20,"MaxPricePer24Hours":0,"StatusMessage":""
            },
            "edvard":{
                "Name":"EdvardGarasjen, Bergen","NumFreeSpaces":113,"NumAvailableChargepoints":4,"NumOccupiedChargepoints":0,"CurrentPrice":42,"CurrentPriceElectric":21,"MaxPricePer24Hours":150
        }
    }
}

Here data has 3 values: "bygarasjen", "klostergarasjen" and "nordnes", but every call those are changing. Here are my tries:

struct Freespace: Decodable {
    var updated: Int
    var status: String
    var data: string
}

struct string: Codable {
    var Name: String
    var NumFreeSpaces: String
    var NumAvailableChargepoints: Int
    var NumOccupiedChargepoints: Int
    var CurrentPrice: Int
    var CurrentPriceElectric: Int
    var MaxPricePer24Hours: Int
    var StatusMessage: String
}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
green8
  • 376
  • 2
  • 14
  • Use a dictionary for the properties with different keys `var data: [String: string]` but please name your custom type something more meaningful than `string` – Joakim Danielson Oct 24 '22 at 12:44
  • I am getting this error: error:typeMismatch(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "data", intValue: nil), _JSONKey(stringValue: "bygarasjen", intValue: nil), CodingKeys(stringValue: "NumFreeSpaces", intValue: nil)], debugDescription: "Expected to decode String but found a number instead.", underlyingError: nil)) – green8 Oct 24 '22 at 12:46
  • Look at the json, the value for that key is clearly an integer – Joakim Danielson Oct 24 '22 at 12:47
  • After changing to Int it still appear as an error – green8 Oct 24 '22 at 12:49
  • I scrolled down and it looks like it can be both so you need to support that, do a search since there are plenty of questions on how to decode something that can be both a string and a number – Joakim Danielson Oct 24 '22 at 12:51

0 Answers0