c# - 如何将多个 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 列表格式是否错误,或者我是否需要更改控制器代码来处理多条记录?
解决方案
推荐阅读
- sql-server - PDO_SQLSRV 在 symfony 中运行很慢?
- python - 有没有办法在同一个环境中安装 CUDA 9 和 10 以同时运行 Tensorflow 2.0 和早期版本?
- google-sheets - 当中间有某个字符时,分别制作图表计数字符串
- macos - Docker:无法启动服务应用程序:OCI 运行时创建失败:
- c# - 我应该使用哪个工具以 Microsoft Speaker Recognition API 支持的格式录制音频(或转换录制的音频)?
- android - 膨胀类 com.google.android.material.navigation.NavigationView 的二进制 XML 错误
- json - 如何在postgresql中查询嵌套json数组
- python - 在 python 中建立和重新连接到 websocket 的更好方法是什么?
- android - 下载文件时,URLConnection 两次命中服务器?
- python - 匹配 EmbedDocuments 中的两个字段