I got json object like this -
and some times like this -
So the order of the object is not fixed. In above example "CreatedOn" field need to stored in DB.
I am using following code to convert it into expandoobject -
JObject jsonObject = JObject.Parse(json);
// eval into an expando
dynamic dynObject = ConvertJTokenToObject(jsonObject);
and here ConvertJTokenToObject -
public 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");
}
Now, the issue is that I dont know which "dimension" element will have "CreatedOn" field.
This works good with the first case -
dynObject.context.custom.dimensions[0].CreatedOn
but breaks in another case, as it should be -
dynObject.context.custom.dimensions[1].CreatedOn
How to search expandoobject by fields name like "CreatedOn" , "Status" etc.