skip to Main Content
{
    "results": [
        {
            "content1": {
                "prop1" : "value1",
                "prop2" : "value2"
            },
            "content2": {
                "prop1" : "value1",
                "prop2" : "value2"
            },
            "contentn": {
                "prop1" : "value1",
                "prop2" : "value2"
            }
        }
    ]
}

I am having trouble converting this json to a dynamic C# object, the case here is that ‘content1’ to ‘content’ will always be different and dynamic content and I do need to convert it to something like a dictionary.
The idea is to reduce the classes as, I will need to create tons of them.

PS: I am using a 3rd party api that’s returning the data as above.

Tried numerous ways to handle this.
JsonConvert to try to map to Dictionary, List and the same for JsonSerializer.Deserialize

4

Answers


  1. The solution below uses System.Text.Json

    Create a Content class:

    public class Content
    {
        public string prop1 { get; set; }
        public string prop2 { get; set; }
    }
    

    and now you can deserialize:

    var data = "{rn    "results": [rn        {rn            "content1": {rn                "prop1" : "value1",rn                "prop2" : "value2"rn            },rn            "content2": {rn                "prop1" : "value1",rn                "prop2" : "value2"rn            },rn            "contentn": {rn                "prop1" : "value1",rn                "prop2" : "value2"rn            }rn        }rn    ]rn}";
    
    var serData = JsonSerializer.Deserialize<Dictionary<string, List<object>>>(data);
    var myResults = serData.First().Value[0].ToString();
    if(!string.IsNullOrEmpty(myResults))
    {
        var myDynamicObjects = JsonSerializer.Deserialize<Dictionary<string, Content>>(myResults);
    }
    
    Login or Signup to reply.
  2. You can try with the ExpandoObject very powerfull for that kind of jobs.

    static async Task Main(string[] _)
    {
        var dynamicReturn = await new HttpClient().GetFromJsonAsync<ExpandoObject>("https://run.mocky.io/v3/dab5a150-40a9-4520-a809-ff5484489fe9");
    
        foreach (var kvp in dynamicReturn)
        {
            Console.WriteLine(kvp.Key + ": " + kvp.Value);
        }
    }
    
    Login or Signup to reply.
  3. you can try this code

    // using Newtonsoft.Json;
    
    Dictionary<string, Content>  content = JObject.Parse(json)["results"][0]
                                              .ToObject<Dictionary<string, Content>>();
     //or
    // using System.Text.Json
    
    Dictionary<string, Content> content = System.Text.Json.Nodes.JsonNode
                                         .Parse(json)["results"][0]
                                         .Deserialize<Dictionary<string, Content>>();
    

    class

    public class Content
    {
        public string prop1 { get; set; }
        public string prop2 { get; set; }
    }
    
    Login or Signup to reply.
  4. try this::

    public class Content
    {
        public string Prop1{ get; set; }
        public string Prop2 { get; set; }
    }
    var people = JsonConvert.DeserializeObject<List<Content>>('your json data');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search