2016-09-22 70 views
1

我有一個類如下,我想它的JSON簽名。這是最後一個屬性Actions,我不確定如何轉移到JSON。我曾嘗試C#到JSON轉換器在線,但它爲Actions爲空。C#到JSON轉換,字典

public class Notification 
    { 
     public string Name { get; set; } 
     public string Body { get; set; } 
     public string Subject { get; set; } 
     public string Users { get; set; } 
     public string Date { get; set; } 
     public List<Dictionary<string,Dictionary<string,string>>> Action { get; set; } 
    } 

在這裏的任何幫助將不勝感激。

+0

您是否嘗試過使用[JavaScriptSerializer.Serialize](https://msdn.microsoft.com/ en-us/library/system.web.script.serialization.javascriptserializer.serialize(v = vs.110).aspx)或[Newtonsoft](http://www.newtonsoft.com/json)? – user2441511

回答

1

該類的JSON是:

{ 
    "Name":"Test", 
    "Body":"TestBody", 
    "Subject":"TestSubject", 
    "Users":"TestUsers", 
    "Date":"9/22/2016 12:26:20 PM", 
    "Action":[ 
     { 
     "key":{ 
      "innerKey":"value" 
     } 
     } 
    ] 
} 

此C#代碼:

var notification = new Notification() 
{ 
    Name = "Test", 
    Body = "TestBody", 
    Subject = "TestSubject", 
    Users = "TestUsers", 
    Date = DateTime.Now.ToString(), 
    Action = new List<Dictionary<string, Dictionary<string, string>>>() 
    { 
     new Dictionary<string, Dictionary<string, string>>() 
     { 
      { "key", new Dictionary<string, string>() { { "innerKey", "value" } } } 
     } 
    } 
};