Create a strongly typed c# object from json object with ID as the name

Make your root object be a dictionary:

var dictionary = JsonConvert.DeserializeObject<Dictionary<string, SessionPerformanceStats>>(theJsonResponse);

Json.NET serializes a dictionary from and to a JSON object, with the keys being converted to the property names. In your case, the ID numbers will be deserialized as the dictionary keys. If you are sure they will always be numbers, you could declare them as such:

var dictionary = JsonConvert.DeserializeObject<Dictionary<long, SessionPerformanceStats>>(theJsonResponse);

See Serialize a Dictionary and Deserialize a Dictionary

Leave a Comment