2

I have this code for getting values out of a json string.

        var json = @"[{""property"":""Status"",""value"":""val""}]";

        var jArray = JArray.Parse(json);

        foreach (JToken jToken in jArray)
        {
            var property = jToken.Value<string>("property");
            var value = jToken.Value<string>("value");             
        }

This works perfect for the provided input. But in some situations the value property may contain an array.

        var json = @"[{""property"":""Status"",""value"":[1,2]}]";

I'd like to check somehow if the value contains a simple value or an array. If the value is an array then bind it to a collection.

Is this possible using JSON.net ?

user49126
  • 1,825
  • 7
  • 30
  • 53
  • 1
    This is a different tack on it, but I use a variation of the code here: http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object. If you like that, I'll post my version of the code (it fixes some bugs, including, I believe, one that involved either contained arrays or contained objects. Can't remember which.) – Pete Oct 25 '13 at 20:08

1 Answers1

6
dynamic value = jToken["value"];
if (value is JArray)
    // do something

(you could use object instead of dynamic in my example, but dynamic might be easier to work with later)

Tim S.
  • 55,448
  • 7
  • 96
  • 122