2017-03-09 53 views
0

當在對象中填充值時,當我序列化OverallMainUserActivity對象時,我無法獲得所需的確切格式。是否有可能從C#中的對象序列化中獲得這種結果?如何在C#中序列化這個特定的json格式

預期的格式:

{ 
"Registration": [ 
    { 
     "1": "10", 
     "2": "2", 
     "3": "20", 
     "4": "20", 
     "5": "20", 
     "6": "20", 
     "7": "20", 
     "8": "20", 
     "9": "20", 
     "10": "20" 
    }], 
"Withdrawal": [ 
    { 
     "1": "10", 
     "2": "2", 
     "3": "20", 
     "4": "20", 
     "5": "20", 
     "6": "20", 
     "7": "20", 
     "8": "20", 
     "9": "20", 
     "10": "20" 
    }] 
} 

我已經試過

public class OverallUserMainActivity 
    { 
    #region Not_suitable 
    public List<KeyValuePair<string, string>> Registration = new KeyValuePair<string, string>(); 
    public List<KeyValuePair<string, string>> Withdrawal = new KeyValuePair<string, string>(); 
    #endregion 

    #region Not_suitable 
    public Dictionary<string, string> Registration = new Dictionary<string, string>(); 
    public Dictionary<string, string> Withdrawal = new Dictionary<string, string>(); 
    #endregion 

    #region Not_suitable 
    public List<RegistrationCls> Registration = new List<Dictionary<string, string>>(); 
    public List<WithdrawalCls> Withdrawal = new List<Dictionary<string, string>>(); 
    #endregion 
    } 

有一種使用陣列來獲得預期的格式以特殊的方式?

+1

如果您正在使用visual studio - 編輯菜單中有「PASTE SPECIAL」選項。將JSON複製到內存中,當您單擊「粘貼JSON」時,這將爲您創建類。 – Programmer

+0

爲什麼不把內部分數放在內部數組中而不是對象? – Bindrid

+0

我不瞭解@Bindrid的建議和第一條評論告訴我一個很好的功能,在VS我沒有注意多年。 –

回答

1

使用List<Dictionary<string, string>>應該工作

Working Fiddle

我只是做了最低限度,以顯示它的工作,你的情況你可能有一些嵌套循環或LINQ查詢您的數據轉化爲List項目和Dictionary項目

+0

對不起,第二次嘗試只是字典,編輯問題 –

+0

謝謝,但有一個方括號中的期望格式是在字典中缺少 –

+1

只是注意到,再次更新哈哈 – maccettura

-1

下面是從選擇性粘貼選項在Visual Studio中輸出碼2015年 -

public class Rootobject 
{ 
    public Registration Registration { get; set; } 
    public Withdrawal Withdrawal { get; set; } 
} 

public class Registration 
{ 
    public string _1 { get; set; } 
    public string _2 { get; set; } 
    public string _3 { get; set; } 
    public string _4 { get; set; } 
    public string _5 { get; set; } 
    public string _6 { get; set; } 
    public string _7 { get; set; } 
    public string _8 { get; set; } 
    public string _9 { get; set; } 
    public string _10 { get; set; } 
} 

public class Withdrawal 
{ 
    public string _1 { get; set; } 
    public string _2 { get; set; } 
    public string _3 { get; set; } 
    public string _4 { get; set; } 
    public string _5 { get; set; } 
    public string _6 { get; set; } 
    public string _7 { get; set; } 
    public string _8 { get; set; } 
    public string _9 { get; set; } 
    public string _10 { get; set; } 
} 
+0

第一位評論者,已發佈。 –

+0

很高興你找到了決議! – Programmer

+0

大聲笑,剛纔注意到,第一個評論和這個答案是從同一個人。 –