I'm trying to ready a JSON file with contents similar to this:
{
"result": {
"1357": {
"icon": "smiley-face",
"name": "happy"
},
"success": true
}
}
This example below works but the problem I'm having is that the property name for classid
is always different. How can I deserialize the classid
without knowing the property name?
using System;
using System.IO;
using Newtonsoft.Json;
public class ClassId
{
[JsonProperty("icon")]
public string Icon { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
public class Result
{
[JsonProperty("1357")] // this key is always different
public ClassId classid { get; set; }
[JsonProperty("success")]
public bool Success { get; set; }
}
public class Example
{
[JsonProperty("result")]
public Result Result { get; set; }
}
class Program
{
static void Main(string[] args)
{
var json = File.ReadAllText("test.json");
var container = JsonConvert.DeserializeObject<Example>(json);
Console.WriteLine(container.Result.classid.Icon);
}
}