Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 446
Somik Raha Avatar asked Jun 01 '17 22:06

Somik Raha


1 Answers

Try to cast the Value property of the JValue:

JValue jv = actualObject as JValue;
if(jv != null)
{
    bool b = (bool)jv.Value;
}
like image 95
mm8 Avatar answered Oct 12 '22 23:10

mm8