C# .net核心API发送数组对象

C# .net核心API发送数组对象,c#,angular,asp.net-core,postman,asp.net-core-webapi,C#,Angular,Asp.net Core,Postman,Asp.net Core Webapi,我有一个学生班,有阵列学生电话。当我将StudentPhones属性从Student类中移除时,当与邮递员一起发布时,该属性工作得非常好。但当添加StudentPhones属性时,Postman会给我以下错误: { “学生电话”:[ 无法将当前JSON数组(例如[1,2,3])反序列化为“DataAccess.StudentPhone”类型,因为该类型需要一个JSON对象(例如{“name\”:“value\”})才能正确反序列化。\r\n若要修复此错误,请将JSON更改为JSON对象(例如{“

我有一个学生班,有阵列学生电话。当我将StudentPhones属性从Student类中移除时,当与邮递员一起发布时,该属性工作得非常好。但当添加StudentPhones属性时,Postman会给我以下错误:

{ “学生电话”:[ 无法将当前JSON数组(例如[1,2,3])反序列化为“DataAccess.StudentPhone”类型,因为该类型需要一个JSON对象(例如{“name\”:“value\”})才能正确反序列化。\r\n若要修复此错误,请将JSON更改为JSON对象(例如{“name\”:“value\”})或者将反序列化类型更改为数组或实现类似集合接口(例如ICollection、IList)的列表的类型,该列表可以从JSON数组反序列化。还可以将JsonArrayAttribute添加到该类型,以强制其从JSON数组反序列化。\r\n路径“StudentPhones”,第4行,位置23。“ ] }


您的JSON中有一个电话数组,因此您的模型中应该有一个电话数组,而不是
public StudentPhone StudentPhones{get;set;}
public List StudentPhones{get;set;}

您的JSON中有一个电话数组,而不是模型中的
public StudentPhone StudentPhone StudentPhones{get;set;}
,你应该让
公共列表StudentPhones{get;set;}

为什么不让
StudentPhones
声明为
StudentPhone
的数组或列表,在
Student
类中?我太粗心了:)。谢谢你的回复。我为这个问题感到羞耻没关系!如果你真的想,你可以删除帖子。它可能会在某个时候被关闭和删除,因为这是代码中的一个简单错误。我尝试删除该问题,但拒绝。为什么不在
学生
类中将
学生电话
声明为
学生电话
的数组或列表?我太粗心了:)。谢谢你的回复。我为这个问题感到羞耻没关系!如果你真的想,你可以删除帖子。它可能会在某个时候被关闭和删除,因为这是代码中的一个简单错误。我尝试删除该问题,但拒绝。您应该使用StudentPhones的列表
公共列表StudentPhones{get;set;}
我太粗心了:)。谢谢你的回复。我为此感到羞愧question@HasanOzdemir我们每个人有时都会遇到这种情况。我将投票否决你的问题。你应该使用学生电话列表
公共列表学生电话{get;set;}
我太粗心了:)。谢谢你的回复。我为此感到羞愧question@HasanOzdemir我们每个人有时都会遇到这种情况。我将投票否决你的问题。
    public class Student
    {
        public string StudentName { get; set; }
        public string StudentSurname { get; set; }
        public StudentPhone StudentPhones { get; set; }
    }

    public class StudentPhone
    {
        public int PhoneType{ get; set; }
        public string PhoneNumber { get; set; }
    }

    public async Task<ServiceResult> SaveStudent([FromBody]Student student)
    {
    }
{
  "studentName": "test",
  "studentSurname": "test",
  "studentPhones": [
    {
      "phoneType": 1,
      "phoneNumber ": "111111111"
    },
    {
      "phoneType": 2,
      "phoneNumber ": "2222222222"
    }
  ],
}