我只是构建了一个测试 Web API,它接受 JSON 有效负载,并将简单地从我的 MVC 控制器发回数组部分中的数据。
这是我填充的类序列化时的当前 JSON 字符串:
{"JobList":[{"ID":1,"Name":"Dave","Age":23,"StartDate":"10/23/2013 6:22:50 AM","JobTitle":"Developer"},{"ID":2,"Name":"John","Age":44,"StartDate":"10/23/2013 6:22:50 AM","JobTitle":"QA"},{"ID":3,"Name":"Dave","Age":23,"StartDate":"10/23/2013 6:22:50 AM","JobTitle":"Senior Developer"}]}
这是用于创建此 JSON 的对象:
var jobList = new JobsList()
{
JobList = new List<Jobs>()
{
new Jobs()
{
Age = 23,
ID = 1,
Name = "Dave",
StartDate = DateTime.Now.ToString(),
JobTitle = "Developer"
},
new Jobs()
{
Age = 44,
ID = 2,
Name = "John",
StartDate = DateTime.Now.ToString(),
JobTitle = "QA"
},
new Jobs()
{
Age = 23,
ID = 3,
Name = "Dave",
StartDate = DateTime.Now.ToString(),
JobTitle = "Senior Developer"
}
}
};
这是我的个人课程:
工作清单类:
public class JobsList:IJobList
{
#region IJobList Members
public List<Jobs> JobList
{
get;
set;
}
#endregion
}
职位类别:
public class Jobs:Interfaces.IJobs
{
#region IJobs Members
public int ID
{
get;
set;
}
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public string StartDate
{
get;
set;
}
public string JobTitle
{
get;
set;
}
#endregion
}
当我通过使用 JSON2Csharp 将我的 JSON 转换为生成类时,生成的类如下所示:
public class JobList
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string StartDate { get; set; }
public string JobTitle { get; set; }
}
public class RootObject
{
public List<JobList> JobList { get; set; }
}
这对我来说似乎不正确。谁能解释为什么我没有在我的 JSON 中将“作业”节点设置为 JobsList 数组?为什么生成的 JSON2CSharp 类将我的“工作”类映射为 JobList 类?