Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to deserialize dictionary with hierarchical keys to dynamic object?
    primarykey
    data
    text
    <p>I'm receiving data in dictionary form and would like to deserialize it into a dynamic object. The twist is the dictionary keys express a hierarchical object. A second twist is some of the keys express an array and/or dictionary property.</p> <p>For example:</p> <pre><code>===========Key================ ======Value======== Person.First | John Person.Last | Doe Person.Phone[Work] | 999-555-1234 Person.Phone[Home] | 999-555-1235 Person.Addresses[Home].Street | 101 My Street Person.Addresses[Home].City | AnyTown Person.Spouse.First | Jane Person.Spouse.Last | Doe Person.Insurance[0].Name | Medicare Person.Insurance[0].GroupNum | 1234567 </code></pre> <p><strong>Edit</strong>: Added array examples</p> <p>I'm trying to find a simple method to create an object that can then be used in a runtime expressions evaluator.</p> <p>Here's my first attempt that works for the simple properties, but doesn't attempt to handle the arrays</p> <pre><code>public static class DynamicBuilder { public static object Build(IDictionary&lt;string, object&gt; dict) { ExpandoObject root = new ExpandoObject(); foreach (var item in dict) { var objDict = (IDictionary&lt;string, object&gt;)root; var parts = item.Key.Split('.'); for (int i = 0; i &lt; parts.Length ; i++) { string propName = parts[i]; if (i &lt; parts.Length - 1) { if (!objDict.ContainsKey(propName)) { dynamic temp = new ExpandoObject(); objDict.Add(propName, temp); objDict = (IDictionary&lt;string, object&gt;)temp; } else { objDict = (IDictionary&lt;string, object&gt;)objDict[propName]; } } else { if (objDict.ContainsKey(propName)) objDict[propName] = item.Value; else objDict.Add(propName, item.Value); } } } return root; } } </code></pre> <p>Any ideas on better way to approach this?</p> <p>Thanks,</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload