Search code examples
c#jsonjsonserializer

How to read JSON value for single value?


I have an application that returns the below JSON format with only 1 value:

{"isActive":true}

I can read the value by putting it into a dictionary as per below:

var value = JsonSerializer.Deserialize<Dictionary<string, bool>>(rawValue, JsonSerializerSettings.Web)!.value;

But it does not seem to be a good way to use a dictionary to store a single key/value.

Is there a better way to get the value from the JSON?


Solution

  • you can just parse your json

    using System.Text.Json;
    
    bool isActive= (bool) JsonNode.Parse(json)["isActive"];
    

    or using Newtonsoft.Json

    using Newtonsoft.Json
    
    bool isActive = (bool) JObject.Parse(json)["isActive"];