Somik Raha
Somik Raha

Reputation: 181

How to cast JValue to bool?

Recently upgraded to Visual Studio 2017 and am facing a weird issue. I am not able to cast Netwonsoft.Json.Linq.JValue to a bool.

For instance, here is my object "actualObj" which contains a bool (shown in QuickWatch): a JValue object containing a bool

Now, when I cast it to a bool, which should have worked as per the docs, I get an invalid cast exception:invalid cast exception

What should I be doing differently?

Upvotes: 7

Views: 3522

Answers (1)

mm8
mm8

Reputation: 169340

Try to cast the Value property of the JValue:

JValue jv = actualObject as JValue;
if(jv != null)
{
    bool b = (bool)jv.Value;
}

Upvotes: 10

Related Questions