Lets assume i have base class and 2 derived classes:
pulic class BaseHumanClass {
public int Id { get; set; }
}
public class Adult : BaseHumanClass {
public string Name { get; set;}
}
public class Child : BaseHumanClass {
public int Age { get; set; }
}
And i’m getting list from api, something like this:
[{ Id: 1, Name: "Dave" }, { Id: 2, Age: 64 }]
How can i use jsonconvert.DeserializeObject to get list with the right derived classes corresponded to the parameters?
Thx
2
Answers
you will need a custom json converter like this
You can use nuget package JsonSubTypes, here is a running sample (https://dotnetfiddle.net/YlUGp5):
See also https://manuc66.github.io/JsonSubTypes/ for usage documentation