如何在C#中序列化这个特定的json格式

时间:2017-03-09 14:35:53

标签: c# json

在对象中填充值时,当我序列化OverallMainUserActivity对象时,我无法获得所需的确切格式。是否可以通过C#中的对象序列化获得此结果?

预期格式:

{
"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 个答案:

答案 0 :(得分:1)

使用List<Dictionary<string, string>>应该有效

Working Fiddle

我只做了最低限度的展示它的工作,在你的情况下,你可能会有一些嵌套循环或LINQ查询将你的数据转换为List项目和Dictionary项目