簡體   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