简体   繁体   中英

How to serialize this specific json format in C#

Upon populating values in the object, When i serialise the OverallMainUserActivity object, i couldnt get the exact format required. Is it possible to have this outcome from serialisation of object in C#?

Expected Format:

{
"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"
    }]
}

I have tried

 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
  }

Is there a special way to use array to get the expected format?

Using List<Dictionary<string, string>> should work

Working Fiddle

I only did the bare minimum to show it working, in your case you would probably have some nested loops or LINQ queries to translate your data into the List items and Dictionary items

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM