Search code examples
c#json.netsteam-web-api

Derserialize JSON.NET With Unknown RootObject Key


{
"578080": {
    "success": true,
    "data": {
        "type": "game",
        "name": "PLAYERUNKNOWN'S BATTLEGROUNDS",
        "steam_appid": 578080,
        "required_age": 0,
        "is_free": false,
        }
    }
}

This is from the Steam API. As you can see the root key the ID itself, so I don't know how to deserialize this to an object. I've seen other questions regarding unknown property names, but can't seem to apply those solutions for when the root name is unknown.


Solution

  • One way to do this is to Deserialize to Dictionary

    Classes

    public class Data
    {
        public string type { get; set; }
        public string name { get; set; }
        public int steam_appid { get; set; }
        public int required_age { get; set; }
        public bool is_free { get; set; }
    }
    
    public class SomeClass
    {
        public bool success { get; set; }
        public Data data { get; set; }
    }
    

    Usage

    var result = JsonConvert.DeserializeObject<Dictionary<string, SomeClass>>(json);