2

I have run into a problem when trying to deserialize JSON using C#.

I have a pagingObject class which has an items object array. This array could be a number of different objects, each having different structures.

class pagingObject
{
    public string href { get; set; }
    public savedTrack[] items { get; set; } //this could be either savedTracks object or Tracks object, depending on request
    public int limit { get; set; }
    public string next { get; set; }
    public int offset { get; set; }
    public string previous { get; set; }
    public int total { get; set; }
}

class savedTrack
{
    public string added_at { get; set; }
    public Track track { get; set; }
}

class Track
{
    public Album album { get; set; }
    public Artist[] artists { get; set; }
    public string[] available_markets { get; set; }
    public int disc_number { get; set; }
    public int duration_ms { get; set; }

    [JsonProperty(PropertyName = "explicit")]
    public bool is_explicit { get; set; }
    public External_ids external_id { get; set; }
    public string href { get; set; }
    public string id { get; set; }
    public string name { get; set; }
    public int popularity { get; set; }
    public string preview_url { get; set; }
    public int track_number { get; set; }
    public string type { get; set; }
    public string uri { get; set; }
}

I am using Newtonsoft.Json to deserialize.

How can i tell my program that items could be either one of the said objects (savedTracks or Tracks)?

Thank you in advance!

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
  • 1
    by letting both classes implement a common base-class and deserialize to a list of this type. – MakePeaceGreatAgain Jan 03 '19 at 14:37
  • but how do i declare this in pagingObjects? in the line with the comment? – Vytas Kondratas Jan 03 '19 at 14:50
  • Do you know what's inside the Json (`savedTrucks` or `Truck`) at the deserialization time? Or it's unknown to you? – Sergey Shevchenko Jan 03 '19 at 15:05
  • Possible duplicate of [Json.net serialize/deserialize derived types?](https://stackoverflow.com/questions/8513042/json-net-serialize-deserialize-derived-types) – StayOnTarget Jan 03 '19 at 15:34
  • Thank you for your help, everyone. I realized that there were not as many possible outcomes, so I just got around the problem by creating seperate pagingObject classes for each of them. This might not be the most clever of solutions but it works. :) – Vytas Kondratas Jan 03 '19 at 16:04

2 Answers2

1

It seems that you may simply use the generic pagingObject<T> as a base model:

class pagingObject<T>
{
    public string href { get; set; }
    public T[] items { get; set; }
    public int limit { get; set; }
    public string next { get; set; }
    public int offset { get; set; }
    public string previous { get; set; }
    public int total { get; set; }
}

And later you may deserialize the JSON by specifying the concrete type, like:

pagingObject<Truck> model = JsonConvert.DeserializeObject<pagingObject<Truck>>(jsonStr);
pagingObject<savedTrack> model = JsonConvert.DeserializeObject<pagingObject<savedTrack>>(jsonStr);
0

Major re-edit, I wasn't thinking straight.

I believe you can change public savedTrack[] items into:

public object[] items

then after you can check if the items array matches either one of the object types you are expecting

Jonas B
  • 2,351
  • 2
  • 18
  • 26