2017-03-09 35 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

大声笑,刚才注意到,第一个评论和这个答案是从同一个人。 –