Skip to content

Instantly share code, notes, and snippets.

@JohannesEH
Created October 25, 2011 09:21
Show Gist options
  • Save JohannesEH/1311997 to your computer and use it in GitHub Desktop.
Save JohannesEH/1311997 to your computer and use it in GitHub Desktop.
JSON to dynamic object function
private static dynamic CreateDynamicFromObjectGraph(object input)
{
if(input is IDictionary<string, object>)
{
var result = (IDictionary<string, object>)(new ExpandoObject());
foreach (var pair in (IDictionary<string, object>)input)
result.Add(pair.Key, CreateDynamicFromObjectGraph(pair.Value));
return result;
}
return input;
}
var json = new JavaScriptSerializer();
dynamic obj = CreateDynamicFromObjectGraph(json.DeserializeObject(someJson));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment