3

Here is my JSON from a remote server....how do I create a C# object for this?

{
    "payload":
    {
        "one":
        {
            "x": 1
        },
        "two":
        {
            "x": 2
        },
        "three":
        {
            "x": 3
        }
    }
}

http://json2csharp.com/ created three classes of type "one", "two" and "three"...but these are dynamic values. I may get "four", "five", "six" on the next request

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
Prashan Pratap
  • 454
  • 1
  • 6
  • 7

1 Answers1

12

You can use Dictionary<string,YourClass>

string json = @"{ ""payload"": { ""one"": { ""x"":1 }, ""two"": { ""x"":2 }, ""three"": { ""x"":3 } } }";
var  root = JsonConvert.DeserializeObject<RootObject>(json);

public class Item
{
    public int x { get; set; }
}

public class RootObject
{
    public Dictionary<string,Item> payload { get; set; }
}
L.B
  • 114,136
  • 19
  • 178
  • 224