Не могу загрузить раздел json в словарь - PullRequest
0 голосов
/ 27 ноября 2018

У меня есть конфиг в 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;
    }

Что здесь не так?

Ответы [ 2 ]

0 голосов
/ 27 ноября 2018

Добавьте код в ваш startup.cs

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

 public IConfiguration Configuration { get; }

Добавьте эту часть в ConfigureServices Method

  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; }
    }
0 голосов
/ 27 ноября 2018

Итак, в первую очередь ваш 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; }
}

Когда у вас есть все это, посмотрите на пакет ньютонов Newtonoft JSON для .NET ииспользуйте метод десериализации.

Он должен выглядеть следующим образом.

var object = JsonConvert.DeserializeObject<DestinationObject>(json);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...