JsonConvert.DeserializeObject(string str)
, when used on a JSON object, returns a JObject
. You can use this directly (e.g. use DeserializeObject<JObject>
to explicitly type it), or as a dynamic
, to access its properties, e.g.
var data = @"{""type"": 1, ""otherProperty"": ""Hello!""}";
dynamic obj = JsonConvert.DeserializeObject(data);
if (obj.type == 1)
{
Console.WriteLine(obj.otherProperty); // prints Hello!
}
Also, you mention numeric fields as being a problem with working with a dictionary, but if you make it a Dictionary<string, dynamic>
you might find it easier to work with:
var data = @"{""type"": 2, ""otherProperty"": 5}";
var dict = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(data);
if (dict["type"] == 2)
{
int i = (int)dict["otherProperty"]; // type is long, so cast is required if you want an int
Console.WriteLine(i); // prints 5
}