简体   繁体   English

使用嵌套 arrays 反序列化 json 时出错

[英]Error deserializing json with nested arrays

I'm trying to deserialize a json object that includes nested arrays and I get the following error when I try it in Postman:我正在尝试反序列化包含嵌套 arrays 的 json object,当我在 Postman 中尝试时出现以下错误:

{ "errors": { "accounts": [ "Cannot deserialize the current JSON array (eg [1,2,3]) into type 'Orders.Dtos.Accounts' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection, IList) like List 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 'accounts', line 7, position 17." { "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": "One or more validation errors occurred.", "status": 400, "traceId": "00-92466567188264cbdc71e6f6d7479688-fe08150cd947ddf1-00" } ] }, "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1", "title": "出现一个或多个验证错误。", "status": 400, " traceId": "00-92466567188264cbdc71e6f6d7479688-fe08150cd947ddf1-00" }

Simplified json to deserialize:简化 json 反序列化:

{
   "plannedShippingDateAndTime": "2023-02-07T07:06:00 GMT+01:00",
   "pickup": {
       "isRequested": false
   },
   "productCode": "N",
   "accounts": [
       {
           "typeCode": "shipper",
           "number": "999999999"
       }
   ]

} }

Classes:班级:

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: 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; }
}

Also Customize JSON binding :自定义 JSON 绑定

options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM