[英]How to read appsettings.json with List of values?
I have the following appSettings.json file我有以下 appSettings.json 文件
"UrlManagement": {
"UrlList": [
{
"Microsoft":{
"resource":"xyz",
type": "iot"
},
"AWS":{
"resource":"abc",
"type": "storage"
},
}]
}
}
I have class我有 class
public class UrlManagement
{
public string CompanyName;
public URLList UrlList;
}
public class URLList
{
public string Resource;
public string Type;
}
I am trying to get these values as follow我试图得到这些值如下
private List<string> GetConfigurationDictionary()
{
var items = this._configuration.GetSection("UrlManagement:UrlList:0").
GetChildren().ToDictionary(x => x.Key, x => x.Value);
}
I am getting null values for x.Value我得到 x.Value 的 null 值
You could search the section UrlManagement:UrlList:0
, like the following code:您可以搜索
UrlManagement:UrlList:0
部分,如以下代码:
private List<UrlManagement> GetConfigurationDictionary()
{
List<UrlManagement> result = this._configuration
.GetSection("UrlManagement:UrlList:0")
.GetChildren()
.ToDictionary(x => x.Key, x => x.Get<UrlList>())
.Select(x => new UrlManagement { CompanyName = x.Key, UrlList = x.Value })
.ToList();
return result;
}
Demo演示
foreach (var item in result)
{
Console.WriteLine($"{item.CompanyName} ==> {item.UrlList.Type}:{item.UrlList.Resource}");
}
Result结果
AWS ==> storage:abc
Microsoft ==> iot:xyz
I hope this helps you fix the issue我希望这可以帮助您解决问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.