【问题标题】:How to read array of dictionary from settings如何从设置中读取字典数组
【发布时间】:2020-08-12 06:11:39
【问题描述】:

我想从 appsettings.json 读取这个设置:

  "CountryPhoneSetting": [
    {
      "US": {
        "DialCode": "+1",
        "CanSMS": true,
        "CanVerify": true
      }
    }
  ]

这是我要存储它的类:

    public class CountryPhoneSetting
{
    public IDictionary<string, CountryDetails> CountryInfo {
        get;set;
    }

    public class CountryDetails
    {
        public string DialCode { get; set; }

        public bool CanSMS { get; set; }

        public bool CanVerify { get; set; }
    }

}

我已经使用了 stackoverflow 解决方案,但它们都不起作用。

请帮忙

【问题讨论】:

  • 我想将它添加到服务中,例如:services.Configure(Configuration.GetSection("CountryPhoneSetting"));
  • 向我们展示无效的代码。
  • Configuration.GetSection("CountryPhoneSetting").GetChildren().ToList() .ToDictionary(x => x.Key, x => x.Value);这导致空
  • 奇怪的是 Json 是 "CountryPhoneSetting": [ { "US": { "DialCode": "+1", "CanSMS": true, "CanVerify": true } } ],额外的[] 使它成为Dictionary&lt;string, CountryPhoneSetting&gt;[] 可以显示一个在字典中有多个条目的配置.. 它会是一个元素的多个字典吗?

标签: c# asp.net json asp.net-core


【解决方案1】:

尝试将您的 json 更改为:

"CountryPhoneSetting": {
    "CountryInfo": {
      "US": {
        "DialCode": "+1",
        "CanSMS": true,
        "CanVerify": true
      },
      "BR": {
        "DialCode": "+55",
        "CanSMS": true,
        "CanVerify": true
      }
    }
  }

还有你的班级:

public class CountryPhoneSetting
{
     public Dictionary<string, CountryDetails> CountryInfo { get; set; } = new Dictionary<string, CountryDetails>();

     public class CountryDetails
     {
        public string DialCode { get; set; }
        public bool CanSMS { get; set; }
        public bool CanVerify { get; set; }
     }
}

用法:

Configuration.GetSection("CountryPhoneSetting").Bind(CountryPhoneSetting);

我已经测试过了,它对我有用!

见:https://weblog.west-wind.com/posts/2017/dec/12/easy-configuration-binding-in-aspnet-core-revisited

【讨论】:

  • 我忘了提到要填充对象 CountryPhoneSetting,您必须将这行代码放入启动文件中:configuration.GetSection("CountryPhoneSetting").Bind(CountryPhoneSetting);如果你不知道怎么做,请阅读这篇文章:weblog.west-wind.com/posts/2017/dec/12/…
  • 这种方法很有用。您能否用您的评论更新答案?
  • @SelimYıldız 看来我没有足够的声誉来编辑答案=(这是我第一次提出问题
  • 发布的答案和分享的文章帮助我解决了我的问题。谢谢!
【解决方案2】:

我为我的项目编写小扩展。你也可以试试

using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Text;

namespace MyProject.Utilities
{
    public static class ConfigurationHelper
    {
        public static T Load<T> (this IConfiguration configuration, string section) where T : new()
        {
            if (typeof(T).IsValueType)
            {
                return LoadStruct<T>(configuration, section);
            }
            return LoadClass<T>(configuration, section);
        }   

        private static T LoadStruct<T>(IConfiguration configuration, string section)
        {
            return configuration.GetSection(section).Get<T>();
        }

        private static T LoadClass<T>(IConfiguration configuration, string section) where T:  new()
        {
            T variable = new T();
            configuration.GetSection(section).Bind(variable);
            return variable;
        }
    }
}

用法:

configuration.Load<List<CountryPhoneSetting>>("CountryPhoneSetting")

编辑:需要的包 - Microsoft.Extensions.Configuration.Binder

另外根据appsettings中的对象,用法必须是这样的:

configuration.Load<List<Dictionary<string, CountryDetails>>>("CountryPhoneSetting")

【讨论】:

    【解决方案3】:

    注意CountryPhoneSetting 之后的额外[]。这是一个字典数组。

    public class CountryPhoneSetting
    {
        [JsonProperty("CountryPhoneSetting")]
        public Dictionary<string, CountryDetails>[]  CountryInfo {get;set;}
    }
    
    public class CountryDetails
    {
        [JsonProperty("DialCode")]
        public string DialCode { get; set; }
        [JsonProperty("CanSMS")]
        public bool CanSms { get; set; }
        [JsonProperty("CanVerify")]
        public bool CanVerify { get; set; }
    }
    
    
    var result  = JsonConvert.DeserializeObject<CountryPhoneSetting>(input);
    

    即使在没有明确定义如何处理重复键条目的情况下将Dictionary&lt;string, CountryDetails&gt;[] 扁平化为Dictionary&lt;string, CountryDetails&gt; 也不容易,也会提供预期的行为

    Live Demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      • 2018-08-27
      • 1970-01-01
      • 2018-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多