Search code examples
c#jsonconvertersexpandoobject

Convert from JSON object to expando object in c#


I have a JSON object, something like:

var jsonObject = {"att1" : "val1","att2" : "val2","att3" : "val3","att4" : "val4"}

I need to convert the same into an ExpandoObject.

I tried something like:

var expConverter = new ExpandoObjectConverter();
dynamic obj = JsonConvert.DeserializeObject<List<ExpandoObject>>(jsonObject, 
                                                                 expConverter);

But it is not giving me the desired result.

Can someone help me get the result?


Solution

  • dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(jsonObject, expConverter);
    

    Your JSON object isn't an array, so you can't serialize to a List<>. Just serialize to the object directly instead.