How can I deserialize a JSON object with dynamic property names?
c#, json
Solution
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; }
}
Problem
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