如何使用值列表读取appsettings.json?

问题描述 投票:1回答:1

我有以下appSettings.json文件

  "UrlManagement": {
   "UrlList": [ 
   {
        "Microsoft":{
            "resource":"xyz",
             type": "iot"
        }, 

        "AWS":{
            "resource":"abc",
             "type": "storage"
        },     
   }]
  }
 }

我上课

public class UrlManagement
{
    public string CompanyName;
    public URLList UrlList;
}

public class URLList 
{
     public string Resource;
     public string Type;
}

我正在尝试获得以下这些值

  private List<string> GetConfigurationDictionary()
  {
        var items = this._configuration.GetSection("UrlManagement:UrlList:0").
            GetChildren().ToDictionary(x => x.Key, x => x.Value);
  }

我正在获得x.Value的空值

c# appsettings
1个回答
0
投票

您可以搜索UrlManagement:UrlList:0部分,然后使用Get<List<UrlList>>()展平所有typeresource,如以下代码:

private Dictionary<string, string> GetConfigurationDictionary()
{
    Dictionary<string, string> keyValuePairs = this._configuration
        .GetSection("UrlManagement:UrlList:0")
        .Get<List<UrlList>>()
        .ToDictionary(x => x.type, y => y.resource);

    return keyValuePairs;
}

我希望这可以帮助您解决问题

© www.soinside.com 2019 - 2024. All rights reserved.