简体   繁体   中英

Initialize dictionary with value (C#)

I have model with Dictionary, that I want to initialize with default value Here is how I do it now

 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..."}}]}
        };

But value is not string

I tried like this

  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..."}}]"}
    };

But it still wrong

How I can solve this issue?

You need to escape " in your string. One approach would be to replace them with \" :

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..."}}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM