I have for example these classes
public class JsonResult
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("myobjects")]
public List<BaseClass> myObjects { get; set; }
}
public abstract class BaseClass
{
[JsonProperty("name")]
public string Name { get; set; }
}
public class FlavourOne: BaseClass
{
[JsonProperty("number")]
public int Number { get; set; }
[JsonProperty("mytext")]
public string MyText { get; set; }
}
public class FlavourTwo: BaseClass
{
[JsonProperty("yourtext")]
public string YourText { get; set; }
}
And i have this Json object coming in as a string
{
"name": "examplename",
"myobjects": [
{
"name": "myFlavourOneInstance",
"number": 1,
"mytext": "Text from One"
},
{
"name": "myFlavourTwoInstance",
"yourtext": "Your Text from Two"
}
]
}
I would like to have an object from that Json string as follows
var responseObject = JsonConvert.DeserializeObject<JsonResult>(rawJsonString);
But this does not work, is there a nice and clean way to have the json into the object with inherited classes and so on?