【问题标题】:Can't load json section into a dictionary无法将 json 部分加载到字典中
【发布时间】:2018-11-27 06:43:29
【问题描述】:

我在 appsettings.json 中有一个配置,例如:

"ClientId": {
      "US": {
        "xxxxxxxx": [ "xxxxxxxx" ]
      },
      "UK": {
        "xxxxxxxx": [ "xxxxxxxx" ]
      },
      "PK": {
        "xxxxxxxx": [ "xxxxxxxx" ]
      },
      "DK": {
        "xxxxxxxx": [ "xxxxxxxx" ]
      },
      "IN": {
        "xxxxxxxx": [ "xxxxxxxx" ]
      },
      "LV": {
        "xxxxxxxx": [ "xxxxxxxx" ]
      },
      "EE": {
        "xxxxxxxx": [ "xxxxxxxx" ]
      }
    }

现在我想将此部分映射到一个字典中,例如:

 public class ClientIds
    {
        public Dictionary<string, List<string>> US { get; set; }
        public Dictionary<string, List<string>> UK { get; set; }
        public Dictionary<string, List<string>> PK { get; set; }
        public Dictionary<string, List<string>> DK { get; set; }
        public Dictionary<string, List<string>> IN { get; set; }
        public Dictionary<string, List<string>> LV { get; set; }
        public Dictionary<string, List<string>> EE { get; set; }
    }

但我每次都得到空值。这是我的启动配置:

  services.Configure<ClientIds>((settings) =>
  {
     Configuration.GetSection("ClientId").Bind(settings);
  });

这是我的服务注入:

    private readonly ClientIds _clientIds;
    public MyService(IOptions<ClientIds> clientIds)
    {
      _clientIds = clientIds.Value;
    }

这里有什么问题?

【问题讨论】:

标签: .net json asp.net-core


【解决方案1】:

所以首先你的 json 应该是这样的:

{
"ClientId": {
      "US": {
        "xxxxxxxx": [ "xxxxxxxx" ]
      },
      "UK": {
        "xxxxxxxx": [ "xxxxxxxx" ]
      },
      "PK": {
        "xxxxxxxx": [ "xxxxxxxx" ]
      },
      "DK": {
        "xxxxxxxx": [ "xxxxxxxx" ]
      },
      "IN": {
        "xxxxxxxx": [ "xxxxxxxx" ]
      },
      "LV": {
        "xxxxxxxx": [ "xxxxxxxx" ]
      },
      "EE": {
        "xxxxxxxx": [ "xxxxxxxx" ]
      }
    }
}

那么你的 c# 类应该是这样的:

public class US
{
    public List<string> xxxxxxxx { get; set; }
}

public class UK
{
    public List<string> xxxxxxxx { get; set; }
}

public class PK
{
    public List<string> xxxxxxxx { get; set; }
}

public class DK
{
    public List<string> xxxxxxxx { get; set; }
}

public class IN
{
    public List<string> xxxxxxxx { get; set; }
}

public class LV
{
    public List<string> xxxxxxxx { get; set; }
}

public class EE
{
    public List<string> xxxxxxxx { get; set; }
}

public class ClientId
{
    public US US { get; set; }
    public UK UK { get; set; }
    public PK PK { get; set; }
    public DK DK { get; set; }
    public IN IN { get; set; }
    public LV LV { get; set; }
    public EE EE { get; set; }
}

public class RootObject
{
    public ClientId ClientId { get; set; }
}

当您了解了适用于 .NET 的 Newtonsoft JSON nuget 包并使用反序列化方法时。

它应该看起来像这样。

var object = JsonConvert.DeserializeObject<DestinationObject>(json);

【讨论】:

    【解决方案2】:

    在您的startup.cs 中添加代码

    public Startup(IConfiguration configuration)
     {
         Configuration = configuration;
     }
    
     public IConfiguration Configuration { get; }
    

    在ConfigureServices方法中添加这部分

      services.Configure<ClientIds>(Configuration.GetSection("ClientId"));
    

    你的ClientIds 班级

        public class ClientIds
        {
            public Dictionary<string, string[]> US { get; set; }
            public Dictionary<string, string[]> UK { get; set; }
            public Dictionary<string, string[]> PK { get; set; }
            public Dictionary<string, string[]> DK { get; set; }
            public Dictionary<string, string[]> IN { get; set; }
            public Dictionary<string, string[]> LV { get; set; }
            public Dictionary<string, string[]> EE { get; set; }
        }
    

    【讨论】:

      猜你喜欢
      • 2020-06-11
      • 2015-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      相关资源
      最近更新 更多