I'm getting a JSON object (may contain multiple levels of JSON arrays and such) which I want to translate into an ExpandoObject.
I figured out how to add simple properties to an ExpandoObject at runtime as it implements IDictionary, but how do I add nested properties (for example, something like myexpando.somelist.anotherlist.someitem
) at runtime that will resolve correctly?
Edit: Currently this works for simple (first level) properties well:
var exo = new ExpandoObject() as IDictionary<String, Object>;
exo.Add(name, value);
The question is how to get the name to be nested and the ExpandoObject to resolve accordingly.
dynamic myexpando = new ExpandoObject();
myexpando.somelist = new ExpandoObject() as dynamic;
myexpando.somelist.anotherlist = new ExpandoObject() as dynamic;
myexpando.somelist.anotherlist.someitem = "Hey Hey There! I'm a nested value :D";
How about doing it like this:
var exo = new ExpandoObject() as IDictionary<String, Object>;
var nested1 = new ExpandoObject() as IDictionary<String, Object>;
exo.Add("Nested1", nested1);
nested1.Add("Nested2", "value");
dynamic d = exo;
Console.WriteLine(d.Nested1.Nested2); // Outputs "value"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With