Newton -反序列化对象时意外的令牌



反序列化对象时意外的令牌:PropertyName。路径的出货量[0]。ShipmentItems',第24行,第28位。

希望有人能帮助或指导我为什么我遇到这个错误时,试图反序列化对象。尽管验证了从restAPI返回的JSON格式正确且有效,但错误仍然存在。我不完全确定我在这里做错了什么或遗漏了什么,任何帮助都会非常感激。

代码部分的片段它是如何被反序列化的

string json = await response.Content.ReadAsStringAsync();
RootObject root = JsonConvert.DeserializeObject<RootObject>(json);

Json部分
{
"shipments": [
{
"shipmentId": 123456789,
"orderNumber": "900678-Z",
"createDate": "2023-04-03T06:51:33.6270000",
"shipDate": "2023-04-10",
"trackingNumber": "NONE",
"shipTo": {
"name": "John D",
"company": "",
"street1": "12223 MAIN STREET",
"street2": "",
"street3": null,
"city": "DALLAS",
"state": "TX",
"postalCode": "12345-1111",
"country": "US",
"phone": "000-000-0000",
"residential": null
},
"advancedOptions": null,
"ShipmentItems": [
{
"orderItemId": 56789403,
"lineItemKey": null,
"sku": "PDOLSQ3785739",
"name": "Potato Kitten -",
"imageUrl": null,
"weight": null,
"quantity": 1,
"unitPrice": 1,
"warehouseLocation": null,
"options": null,
"productId": 1065998,
"fulfillmentSku": null
}
],
"labelData": null,
"formData": null
}
],
"total": 1,
"page": 1,
"pages": 0
}

类模型
public partial class RootObject
{
[JsonProperty("shipments")]
public List<Shipment> Shipments { get; set; }
[JsonProperty("total")]
public int Total { get; set; }
[JsonProperty("page")]
public int Page { get; set; }
[JsonProperty("pages")]
public int Pages { get; set; }
}
public partial class Shipment
{
[JsonProperty("shipmentId")]
public long ShipmentId { get; set; }
[JsonProperty("orderNumber")]
public string OrderNumber { get; set; }
[JsonProperty("createDate")]
public DateTimeOffset CreateDate { get; set; }
[JsonProperty("shipDate")]
public DateTimeOffset ShipDate { get; set; }
[JsonProperty("trackingNumber")]
public string TrackingNumber { get; set; }
[JsonProperty("shipTo")]
public ShipTo ShipTo { get; set; }
[JsonProperty("advancedOptions")]
public object AdvancedOptions { get; set; }
[JsonProperty("shipmentItems")]
public List<ShipmentItem> ShipmentItems { get; set; }
[JsonProperty("labelData")]
public object LabelData { get; set; }
[JsonProperty("formData")]
public object FormData { get; set; }
}
public partial class ShipTo
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("company")]
public string Company { get; set; }
[JsonProperty("street1")]
public string Street1 { get; set; }
[JsonProperty("street2")]
public string Street2 { get; set; }
[JsonProperty("street3")]
public object Street3 { get; set; }
[JsonProperty("city")]
public string City { get; set; }
[JsonProperty("state")]
public string State { get; set; }
[JsonProperty("postalCode")]
public string PostalCode { get; set; }
[JsonProperty("country")]
public string Country { get; set; }
[JsonProperty("phone")]
public string Phone { get; set; }
[JsonProperty("residential")]
public bool Residential { get; set; }
}
public partial class ShipmentItem
{
[JsonProperty("orderItemId")]
public long OrderItemId { get; set; }
[JsonProperty("lineItemKey")]
public string LineItemKey { get; set; }
[JsonProperty("sku")]
public string Sku { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("imageUrl")]
public uri ImageUrl { get; set; }
[JsonProperty("weight")]
public double Weight { get; set; }
[JsonProperty("quantity")]
public in Quantity { get; set; }
[JsonProperty("unitPrice")]
public double UnitPrice { get; set; }
[JsonProperty("warehouseLocation")]
public string WarehouseLocation { get; set; }
[JsonProperty("options")]
public string Options { get; set; }
[JsonProperty("productId")]
public long ProductId { get; set; }
[JsonProperty("fulfillmentSku")]
public string FulfillmentSku { get; set; }
}

您需要将这些属性更改为nullable,因为它们在json字符串中具有空值。如果json字符串

中不能为空,那么检查另一个不可为空属性也是有意义的
[JsonProperty("residential")]
public bool? Residential { get; set; }
.....
[JsonProperty("weight")]
public double? Weight { get; set; }

最新更新