This particular set up continues to elude me. I'm on .NET Core 6, so all of the set up stuff is gone. I'm left with the code below (I think). I have a json array of objects. I've seen where people say that this is a dictionary, so I've tried this, but the values within the array don't populate.
I'm able to get simpler objects to populate their values. I've also tried this pattern according to other SO posts, but no dice.
I keep getting close, but no cigar - What am I doing wrong?
Appsettings.json:
"TimeSlotMenuIds": [
{
"FounderWallEvening": 900000000001136943
},
{
"BravoClubEvening": 900000000001136975
}
]
My mapping class:
public class TimeSlotMenuIds
{
public Dictionary<string, long> TimeSlotMenuId { get; set; }
}
The thing that doesn't populate the values from my json file:
var test = _configuration.GetSection("TimeSlotMenuIds").Get<TimeSlotMenuIds[]>();
var t2 = _configuration.GetSection("TimeSlotMenuIds").GetChildren();
CodePudding user response:
Your JSON structure doesn't really lend itself to being deserialised as a Dictionary directly, instead it would work as an array of dictionaries e.g. Dictionary<string, long>[]
. I'm sure you don't want this, so an option would be to manually process the configuration:
var test = Configuration.GetSection("TimeSlotMenuIds")
.GetChildren()
.ToDictionary(
x => x.GetChildren().First().Key,
x => long.Parse(x.GetChildren().First().Value));
Though I would suggest this is a nasty hack. Instead, you should fix the JSON to something like this:
"TimeSlotMenuIds": {
"FounderWallEvening": 900000000001136943,
"BravoClubEvening": 900000000001136975
}
Which would then allow you to do this:
var test = Configuration.GetSection("TimeSlotMenuIds").Get<Dictionary<string, long>>();