0

I'm trying to call some post API which accepts the payload in following format.

{
 "interval_from": "2001",
 "interval_to": "2011",
 "shift_1": "S1",
 "shift_2": "S2",
 "shift_n": "value"
}

So I created a class called Interval.

class Interval {
  [JsonProperty('interval_from')] <--- Annotation from Newtonsoft.json
  public string from {get; set;}

  [JsonProperty('interval_to')]
  public string to {get; set;}
      
  [JsonProperty]
  public Dictionary<string, string> shifts {get; set;}

}

But Interval class cannot serialized into expected JSON format. Dictionary creates a new object inside Interval and adds respective values in it. It serializes as:

 {
     "interval_from": "2001",
     "interval_to": "2011",
     "shifts": {
       "shift_1": "S1",
       "shift_2": "S2",
       "shift_n": "value"
     }
}

And there is generic method to call API's, Where serialization logic is written in the same method.

Should I use anything apart from Dictionary? Is there any way to get expected serialize json ?

Tushar Pol
  • 7,119
  • 14
  • 29
  • 36

0 Answers0