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 ?

1
  • 1
    This is a different tack on it, but I use a variation of the code here: stackoverflow.com/questions/3142495/…. 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.) Commented Oct 25, 2013 at 20:08

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

1 Comment

FYI, you can also use if (value.Type == JTokenType.Array).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.