I am trying to read an appSettings.json configuration file using .Net Core 6.0 console application and retrieve a section with the values in the original order as they appear in the file.
With the given appSettings.cs file:
I have tried to put it in various formats of arrays and objects but no changes. Here are the variants I have tried:
{
"Section0": {
"D": "1",
"B": "2",
"A": "3",
"C": "4"
},
"Section1": [
"D:1",
"B:2",
"A:3",
"C:4"
],
"Section2": [
{
"D": "1",
"B": "2",
"A": "3",
"C": "4"
}
],
"Section3": [
{
"D": "1"
},
{
"B": "2"
},
{
"A": "3"
},
{
"C": "4"
}
]
}
I have tried the following code:
using Microsoft.Extensions.Configuration;
var configuration = new ConfigurationBuilder().AddJsonFile("appSettings.json", true, true).Build();
var dictionary = configuration.GetSection("Section").GetChildren().ToDictionary(data => data.Key, data => data.Value);
but the resulting dictionary looks like this: image of dictionary object contents from debugger
I also tried a list rather than a dictionary:
var list = configuration.GetSection("Section").AsEnumerable().ToList();
But it also does not preserve the order: image of the list object contents from debugger
Any ideas to get the section contents in the original order?