Search code examples
c#jsonserializationjson-deserialization

Howto C# DeserializeObject, where variable name is number?


How do I Deserialize the following. The problem is that the variable name is a number. So how should MyClass be defined?

json_str:
{"23521952": {"b": [], "o": []}, "23521953": {"b": [], "o": []}}

class MyClass {     //? };

var var = JsonConvert.DeserializeObject<MyClass>(json_str);

Solution

  • This sounds like the outer object is actually a dictionary:

    using System.Collections.Generic;
    using Newtonsoft.Json;
    
    class Foo
    {
        // no clue what b+o look like from the question; modify to suit
        public int[] b { get; set; }
        public string[] o { get; set; }
    }
    static class P
    {
        static void Main()
        {
            var json = @"{""23521952"": {""b"": [], ""o"": []}, ""23521953"": {""b"": [], ""o"": []}}";
            var obj = JsonConvert.DeserializeObject<Dictionary<string, Foo>>(json);
            foreach(var pair in obj)
            {
                System.Console.WriteLine($"{pair.Key}, {pair.Value}");
            }
    
        }
    }