[英]Error deserializing json with nested arrays
我正在尝试反序列化包含嵌套 arrays 的 json object,当我在 Postman 中尝试时出现以下错误:
{ "errors": { "accounts": [ "无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型 'Orders.Dtos.Accounts',因为该类型需要一个 JSON object(例如 {"name" :"value"}) 以正确反序列化。\r\n要修复此错误,请将 JSON 更改为 JSON object(例如 {"name":"value"})或将反序列化类型更改为数组或实现的类型集合接口(例如 ICollection、IList),如 List,可以从 JSON 数组反序列化。也可以将 JsonArrayAttribute 添加到类型以强制它从 JSON 数组反序列化。\r\n路径“帐户”,第 7 行,position 17" ] }, "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1", "title": "出现一个或多个验证错误。", "status": 400, " traceId": "00-92466567188264cbdc71e6f6d7479688-fe08150cd947ddf1-00" }
简化 json 反序列化:
{
"plannedShippingDateAndTime": "2023-02-07T07:06:00 GMT+01:00",
"pickup": {
"isRequested": false
},
"productCode": "N",
"accounts": [
{
"typeCode": "shipper",
"number": "999999999"
}
]
}
班级:
public class ShipmentData
{
public string PlannedShippingDateAndTime { get; set; }
public Pickup Pickup { get; set; }
public string ProductCode { get; set; } = "N";
public Accounts Accounts { get; set; }
}
public class Pickup
{
public bool isRequested { get; set; }
}
public class Accounts
{
public ArrayAccounts[] ArrayAccounts { get; set; }
}
public class ArrayAccounts
{
public string TypeCode { get; set; }
public string Number { get; set; }
}
Api Controller:
[HttpPost("CreateShipment/{OrderId}")]
public async Task<ActionResult> CreateShipment(string OrderId, ShipMentData shipmentData)
{
...
}
public class ShipmentData
{
public string PlannedShippingDateAndTime { get; set; }
public Pickup Pickup { get; set; }
public string ProductCode { get; set; }
public Account[] Accounts { get; set; }
}
public class Pickup
{
public bool IsRequested { get; set; }
}
public class Account
{
public string TypeCode { get; set; }
public string Number { get; set; }
}
options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.