I have 2 C# class:
public class Light
{
public int Brightness { get; set; }
public string Mode { get; set; }
}
public class AirConditioner
{
public int Temperature{ get; set; }
public string Mode { get; set; }
}
JSON file format:
{
"Light": {
"Brightness": 5,
"Mode": "On"
},
"AirConditioner": {
"Temperature": 25,
"Mode": "Cooling"
}
}
I want to parse JSON file to C# by section, something like this:
var light = JsonDeserialize<Light>.(FileSection["Light"]);
var aircon = JsonDeserialize<AirConditioner>.(FileSection["AirConditioner"]);
What I want is the same as Asp.Net Core configuration work:
var light = new Light();
Configuration.GetSection("Light").Bind(light);
It will be better if I do not need to install other packages.
Thank you for your help.
Update: The problem is how to get a section of the JSON file. If I can get the Light section like this:
var lightString = JsonFile.GetSection("Light");
Then I can simply deserialize with System.Text.Json namespace:
var light = JsonSerializer.Deserialize<Light>(lightString);