Инициализировать словарь значением (C#) - PullRequest
0 голосов
/ 19 июня 2020

У меня есть модель со словарем, которую я хочу инициализировать со значением по умолчанию Вот как я это делаю сейчас

 public Dictionary<string, string> controllableProperties =  new Dictionary<string, string> {
         {"controllableProperties", [{"name": "Reboot","type": {"@class": "com.avispl.symphony.api.dal.dto.control.AdvancedControllableProperty$Button","gracePeriod": "120000","labelPressed": "Rebooting..."}}]}
        };

Но значение не является строкой

Я пробовал вот так

  public Dictionary<string, string> controllableProperties =  new Dictionary<string, string> {
     {"controllableProperties", "[{"name": "Reboot","type": {"@class": "com.avispl.symphony.api.dal.dto.control.AdvancedControllableProperty$Button","gracePeriod": "120000","labelPressed": "Rebooting..."}}]"}
    };

Но все равно не так

Как я могу решить эту проблему?

1 Ответ

0 голосов
/ 19 июня 2020

Вам нужно экранировать " в вашей строке. Один из подходов - заменить их на \":

Dictionary<string, string> controllableProperties = new Dictionary<string, string>
{
    {
        "controllableProperties",
        "[{\"name\": \"Reboot\",\"type\": {\"@class\": \"com.avispl.symphony.api.dal.dto.control.AdvancedControllableProperty$Button\",\"gracePeriod\": \"120000\",\"labelPressed\": \"Rebooting...\"}}]"
    }
};
Console.WriteLine(controllableProperties.Values.First()); // prints [{"name": "Reboot","type": {"@class": "com.avispl.symphony.api.dal.dto.control.AdvancedControllableProperty$Button","gracePeriod": "120000","labelPressed": "Rebooting..."}}]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...