无法将json节加载到字典中

时间:2018-11-27 06:43:30

标签: .net json asp.net-core

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

 for(var i=0;i<s.BdayDetails.length;i++){

现在,我想将此部分映射到像这样的字典中:

"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);
  });

这是怎么了?

2 个答案:

答案 0 :(得分:0)

因此,您的json首先应如下所示:

ValueA   ValueA    ValueB
nanmean  nanmin    nanmax    Start      Stop

nan      nan       nan        0          100
8        1         21         101        200
nan      nan       nan        666        1000

然后您的c#类应如下所示:

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

了解完.NET的Newtonsoft JSON nuget包并使用Deserialize方法后。

应该看起来像这样。

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; }
}

答案 1 :(得分:0)

在您的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; }
    }