首页 > 解决方案 > 如何将多个 JSON 列表项传递给 HttpPost?

问题描述

我在 C# .NET Web API 中实现了一个 API

有一个员工班 -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebPersonCacheAPI.Model
{
    public class Employee
    {


        public string EmployeeID { get; set; }

        public string Surname { get; set; }

        public string WindowsID { get; set; }

        public string Forenames { get; set; }

        public string PracticeCode { get; set; }

        public string SMTPAddress { get; set; }

        public string LineManager { get; set; }

        public string PracticeAdmin { get; set; }

        public string LeaveDate { get; set; }

        public string JobTitle { get; set; }

        public string HireDate { get; set; }

        public string Grade { get; set; }

        public decimal? RequiredHours { get; set; }

        public string BusinessTelephone { get; set; }

        public string MobileNumber { get; set; }

        public string ExtensionNumber { get; set; }

        public string DepartmentName { get; set; }

        public string Country { get; set; }

        public string DistinctName { get; set; }

        public string GradeDesc { get; set; }

        public string OfficeName { get; set; }

        public string UnitName { get; set; }

        public string Rank { get; set; }
    }
}

以及将发布的记录保存到数据库表的控制器:

  // POST: api/Employees

    [HttpPost]
   
    public async Task<IActionResult> PostEmployee([FromBody] Employee employee)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        _context.Employees.Add( employee);
        await _context.SaveChangesAsync();
        
        return CreatedAtAction("GetEmployee", new { id = employee.EmployeeID }, employee);
        
    }

当我使用 Postman 发布一条 JSON 记录时,一切正常:

{
         "EmployeeID" : "PAC282811",
         "Surname" : "Smith",
         "WindowsID" : "CAMIS2",
         "Forenames" : "Ian",
         "PracticeCode" : "GS001",
         "SMTPAddress" : "ian.smith@company.com",
         "LineManager" : "PAC233455",
         "PracticeAdmin" : "PAC455322", 
         "LeaveDate" : "20/12/2020",
         "JobTitle" : "Developer",
         "HireDate" : "01/01/2001",
         "Grade" : "AS(3)",
         "RequiredHours" : 37,
         "BusinessTelephone" : "01763456456",
         "MobileNumber" : "07777237276",
         "ExtensionNumber" : "34433",
         "DepartmentName" : "Group Systems",
         "Country" : "UK(1)",
         "DistinctName" : "Ian Smith",
         "GradeDesc" : "AS(3)",
         "OfficeName" : "London",
         "UnitName" : "Group Systems",
         "Rank" : "AS(3)"
}

但是,我需要发布多条记录,所以我尝试使用此 JSON 代码进行 API 调用

[
    {
         "EmployeeID" : "PAC282811",
         "Surname" : "Smith",
         "WindowsID" : "CAMIS2",
         "Forenames" : "Ian",
         "PracticeCode" : "GS001",
         "SMTPAddress" : "ian.smith@company.com",
         "LineManager" : "PAC233455",
         "PracticeAdmin" : "PAC455322", 
         "LeaveDate" : "20/12/2020",
         "JobTitle" : "Developer",
         "HireDate" : "01/01/2001",
         "Grade" : "AS(3)",
         "RequiredHours" : 37,
         "BusinessTelephone" : "01763456456",
         "MobileNumber" : "07777237276",
         "ExtensionNumber" : "34433",
         "DepartmentName" : "Group Systems",
         "Country" : "UK(1)",
         "DistinctName" : "Ian Smith",
         "GradeDesc" : "AS(3)",
         "OfficeName" : "Cambridge",
         "UnitName" : "Group Systems",
         "Rank" : "AS(3)"
},

    {
         "EmployeeID" : "PAC12221",
         "Surname" : "Jones",
         "WindowsID" : "CAMDJ1",
         "Forenames" : "David",
         "PracticeCode" : "GS001",
         "SMTPAddress" : "david.jones@company.com",
         "LineManager" : "PAC233455",
         "PracticeAdmin" : "PAC455322", 
         "LeaveDate" : "18/01/2021",
         "JobTitle" : "Developer",
         "HireDate" : "01/01/2001",
         "Grade" : "AS(3)",
         "RequiredHours" : 37,
         "BusinessTelephone" : "01763456456",
         "MobileNumber" : "07777237276",
         "ExtensionNumber" : "34433",
         "DepartmentName" : "Group Systems",
         "Country" : "UK(1)",
         "DistinctName" : "David Jones",
         "GradeDesc" : "AS(3)",
         "OfficeName" : "Cambridge",
         "UnitName" : "Group Systems",
         "Rank" : "AS(3)"
}
]

我收到以下错误...

 "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'WebPersonCacheAPI.Model.Employee' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON object (e.g. {\"name\":\"value\"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\r\nPath '', line 1, position 1."

我的 JSON 列表格式是否错误,或者我是否需要更改控制器代码来处理多条记录?

标签: c#jsonasp.net-web-api

解决方案


推荐阅读