【发布时间】:2019-09-14 11:02:45
【问题描述】:
"Message": "请求无效。", “模型状态”:{ “通知”:[
"无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型 'iBasement.Models.Notification',因为该类型需要 JSON 对象(例如 {\"name\":\"value\" }) 以正确反序列化。\r\n要修复此错误,请将 JSON 更改为 JSON 对象(例如 {\"name\":\"value\"})或将反序列化的类型更改为数组或实现的类型一个集合接口(例如 ICollection、IList),例如可以从 JSON 数组反序列化的 List。也可以将 JsonArrayAttribute 添加到该类型以强制它从 JSON 数组反序列化。\r\nPath '',第 1 行,第 1 位。”
尝试使用这样的数组调用帖子:
POST /api/Notifications/UpdateMac HTTP/1.1
Host: localhost:56005
Content-Type: application/json
User-Agent: PostmanRuntime/7.17.1
Accept: */*
Cache-Control: no-cache
Postman-Token: 4ac88367-af2c-48e8-99a0-b89fba2ea76a,c1f78db8-e040-4675-9103-5c9c41273c24
Host: localhost:56005
Accept-Encoding: gzip, deflate
Content-Length: 507
Connection: keep-alive
cache-control: no-cache
[
{
"Id": 1,
"MacAddress": "f8:f0:05:ed:1d:28",
"Destination": "janedoe@gmail.com",
"SendAlertTypes": "0,1,4,5,6"
},
{
"Id": 6,
"MacAddress": "f8:f0:05:ed:1d:28",
"Destination": "johndoe@hotmail.com",
"SendAlertTypes": "0,1,2,3,4,5,6"
},
{
"Id": 99,
"MacAddress": "f8:f0:05:ed:1d:28",
"Destination": "4012221234@vtext.com",
"SendAlertTypes": "0,1,2,3,4,5,6"
}
]
[Route("~/UpdateMac")]
public async Task<HttpResponseMessage> PostNotifications([FromBody]List<Notification> notifications)
{
//do list operations..
}
public class Notification
{
public int Id { get; set; }
public string MacAddress { get; set; }
public string Destination { get; set; }
//"0,1,2,3,6" <-- 4, 5, & 7+ would be omitted
public string SendAlertTypes { get; set; }
}
【问题讨论】:
-
您是否有机会实际发送
{"notification": [....]}而不是实际的[..]? -
@OrelEraki 不,这正是我的 PostMan 消息中当前发送正文的方式
-
介意向我们展示正在发送的原始请求的图片吗? (正文和标题)
-
iBasement.Models.Notification 中的结构是什么。
-
@OrelEraki 添加信息