我有一个来自API的JSON响应,该API可能包含不同的内容类型(不同的内容类型需要不同的对象)。 API响应中的JSON如下所示:
{
"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
}
}
]
}
以及以下对象:
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; }
}
在处理响应时,如何动态地在对象类型之间切换?我尝试将Content属性设置为对象,使接口动态化,但是我没有主意,我真的不知道是否有可能处理这种情况。