If you are on C# 4, try expando objects so you can assign any properties as needed and serialize them to json using JSON.Net.
And using the following code, you will also be able to convert a JSON object to a dynamic object in C#
public static class DynamicUtils
{
public static object ConvertJTokenToObject(JToken token)
{
if (token is JValue)
{
return ((JValue)token).Value;
}
if (token is JObject)
{
ExpandoObject expando = new ExpandoObject();
(from childToken in ((JToken)token) where childToken is JProperty select childToken as JProperty).ToList().ForEach(property =>
{
((IDictionary<string, object>)expando).Add(property.Name, ConvertJTokenToObject(property.Value));
});
return expando;
}
if (token is JArray)
{
object[] array = new object[((JArray)token).Count];
int index = 0;
foreach (JToken arrayItem in ((JArray)token))
{
array[index] = ConvertJTokenToObject(arrayItem);
index++;
}
return array;
}
throw new ArgumentException(string.Format("Unknown token type '{0}'", token.GetType()), "token");
}
}
Usage sample:
var parsed = JObject.Parse(response.Content);
dynamic result = DynamicUtils.ConvertJTokenToObject(parsed);