我想将此 JSON 映射到 .NET 类。如何将此 JSON 数据映射到类中?请建议如何。这是 JSON:
{"results": [
"43853",
"43855",
"43856",
"43857",
{
"questionType": 3,
"choiceAnswers": [123]
}
]}
最简单的解决方案是使用 Visual Studio 编辑 > 选择性粘贴 > 将 Json 粘贴为类。 但由于您的 json 是不同对象的数组,因此 .NET 类将是
public class JsonDto
{
public List<object> Results { get; set; }
}
对象列表使用起来会很痛苦,所以我建议您使用类型化模型,但是您需要指定需要定义值,这是一个示例
{"results": [
{
"key1":"43853",
"key2":"43855",
"key3":"43856",
"key4":"43857",
"question": {
"questionType": 3,
"choiceAnswers": [123]
}
}
]};
public class JsonDto
{
public List<ResultDto> Results { get; set; }
}
public class ResultDto
{
public string Key1 { get; set; }
public string Key2 { get; set; }
public string Key3 { get; set; }
public string Key4 { get; set; }
public QuestionDto Question { get; set; }
}
public class QuestionDto
{
public int QuestionType { get; set; }
public List<int> ChoiceAnswers { get; set; }
}
您可以使用在线转换器将 json 数据转换为 c# 模型http://json2csharp.com对于您的 json 来说,它会是这样的。
public class RootObject
{
public List<object> results { get; set; }
}