I have JSON response from an API which might contain a different content type (different content type requires a different object). This is how the JSON from the API response looks like:
{
"Id": 2,
"UserContent": [{
"Id": 10,
"ContentType": "1",
"Content": [{
"Id": "7",
"Question": "Question 1?",
"Answer": "Answer 1."
}, {
"Id": "1",
"Question": "Question 2?",
"Answer": "Answer 2."
}, {
"Id": "6",
"Question": "Question 3?",
"Answer": "Answer 3."
}
]
}, {
"Id": 11,
"ContentType": "2",
"Content": {
"TestScore": "73",
"TestIsPassed": false
}
}
]
}
And the following objects:
public class ContentDataTransferModel
{
public int Id { get; set; }
public List<ContentDataModel> UserContent { get; set; }
}
public class ContentDataModel
{
public int Id { get; set; }
public string ContentType { get; set; }
public WhatShouldBeTheContentType? Content { get; set; }
}
public class ContentDataType1
{
public int Id { get; set; }
public string Question { get; set; }
public string Answer { get; set; }
}
public class ContentDataType2
{
public string TestScore { get; set; }
public bool TestIsPassed { get; set; }
}
How can I dynamically switch between the object types when I am handling the response or is that even possible? I tried setting the Content property to object, dynamic an interface but I am out of ideas and I don't really know if it's possible to handle such a scenario.