繁体   English   中英

用值初始化字典(C#)

[英]Initialize dictionary with value (C#)

我有带字典的 model,我想用默认值初始化这是我现在的做法

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

但还是错了

我该如何解决这个问题?

您需要在字符串中转义" 。一种方法是用\"替换它们:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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