从appsettings.json读取列表<(string,string[])>



我需要阅读"产品"(string,string[])>变量。appsettings.json:

{
"Products": [
{"fruits":["apple","cherry","tomato"]},
{"vegetables":["carrot","tomato"]}
]
}

注意:请假定我无法更改此应用设置。Json输入格式。但是变量也可以是ValueTuple的数组,而不是ValueTuple的List。ValueTuple格式必须为(string category,string[] items)。

感谢@Serge的精彩回答!我相信我不是一个人在linq和那个配置文件中挣扎。

try this

var products = Configuration
.GetSection("Products")
.Get<Dictionary<string, string[]>[]>()
.SelectMany(i => i)
.Select(i => new Tuple<string, string[]>(i.Key, i.Value)) // or you can convert here to any class
.ToList();

相关内容

最新更新