string json = "{ \"isActive\": true }"; JObject jsonObject = JObject.Parse(json); bool isActive = (bool)jsonObject.GetValue("isActive"); Console.WriteLine(isActive); // Output: True
string json = "{ \"person\": { \"name\": \"John\", \"isActive\": false } }"; JObject jsonObject = JObject.Parse(json); bool isActive = (bool)jsonObject.SelectToken("person.isActive"); Console.WriteLine(isActive); // Output: FalseIn both examples, we first create a JSON object from a string using the `JObject.Parse` method. Then, we use the `GetValue` or `SelectToken` method to retrieve the boolean value associated with a specific key. Finally, we cast the value to a boolean and store it in a variable. Overall, the `GetBoolean` method is a useful tool for working with JSON data in C#, and is part of the widely used Newtonsoft.Json package library.