Tuesday, June 9, 2026
HomeiOS Developmentios - Codable: parse JSON with dynamic struct

ios – Codable: parse JSON with dynamic struct

[ad_1]

I’m making app and I’m taking information from http however for testing copied and pasted instance into file and I do not know easy methods to create mannequin when one in all values is altering each name, however its not in an array.
Right here is json file:

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

Right here information has 3 values: “bygarasjen”, “klostergarasjen” and “nordnes”, however each name these are altering.
Listed here are my tries:

struct Freespace: Decodable {
    var up to date: Int
    var standing: String
    var information: string
}

struct string: Codable {
    var Title: String
    var NumFreeSpaces: String
    var NumAvailableChargepoints: Int
    var NumOccupiedChargepoints: Int
    var CurrentPrice: Int
    var CurrentPriceElectric: Int
    var MaxPricePer24Hours: Int
    var StatusMessage: String
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments