[英]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.