0

I have this class:

  public class Result
    {
        public int RegionId { get; set; }
        public string LeagueName { get; set; }
        public string Id { get; set; }
        public string RegionName { get; set; }
        public DateTime Date { get; set; }
        public string SportName { get; set; }
        public bool IsPreMatch { get; set; }
        public int SportId { get; set; }
        public string AwayTeam { get; set; }
        public string HomeTeam { get; set; }
        public string updated_at { get; set; }
        public int LeagueId { get; set; }
        public int? BetRadarId { get; set; }
        public List<Market> Markets { get; set; }
        public int our_event_id { get; set; }
    }

But when the Market is empty, the API I depend on returns "Markets" : {} instead of "Markets": [] so it can't be deserialized. How do I fix this?

Dinter
  • 1
  • 1
  • This is pretty common. `JsonConvert.DeserializeObject` should handle this on its own. What Json.Net version are you using? Can you show your deserialization method? – Jimi Sep 12 '20 at 13:15
  • Looks like API should be fixed. Btw, here's a [link](https://stackoverflow.com/q/25148172/12888024) – aepot Sep 12 '20 at 15:14
  • You can apply `[JsonConverter(typeof(TolerantCollectionConverter))]` to `public List Markets { get; set; }`, where `TolerantCollectionConverter` comes from [this answer](https://stackoverflow.com/a/47318382/3744182) to [Handling JSON response which sometimes doesn't have array](https://stackoverflow.com/q/47317792/3744182). In fact I think this is a duplicate, agree? – dbc Sep 12 '20 at 15:38
  • Yes that works, see https://dotnetfiddle.net/sUuNGT. – dbc Sep 12 '20 at 15:49

0 Answers0