You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JSON反序列化异常求助:Unexpected token while deserializing object

反序列化JSON报错:Unexpected token while deserializing object: PropertyName

问题描述

反序列化对象时遇到错误:

Unexpected token while deserializing object: PropertyName. Path 'Shipments[0].ShipmentItems', line 24, position 28.

已验证REST API返回的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
}

C#类模型

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

错误排查与修复方案

1. JSON字段与JsonProperty名称不匹配

JSON中对应字段是"ShipmentItems"(首字母大写S),但Shipment类里的JsonProperty标注的是"shipmentItems"(小写s),两者完全不匹配,这是触发错误的核心原因。

修改Shipment类的JsonProperty:

[JsonProperty("ShipmentItems")]
public List<ShipmentItem> ShipmentItems { get; set; }

2. 值类型无法接收null值

  • ShipTo类的Residential是bool类型,但JSON中该字段值为null,值类型不能为null,需改为可空布尔类型:
[JsonProperty("residential")]
public bool? Residential { get; set; }
  • ShipmentItem类的Weight是double类型,JSON中为null,改为可空类型:
[JsonProperty("weight")]
public double? Weight { get; set; }

3. 语法错误与类型错误

  • ShipmentItem类里的uri是小写,C#中应为Uri,且JSON中值为null,需改为可空类型:
[JsonProperty("imageUrl")]
public Uri? ImageUrl { get; set; }
  • ShipmentItem类的Quantity属性,类型写成了in,这是语法错误,应改为int:
[JsonProperty("quantity")]
public int Quantity { get; set; }

4. 其他潜在问题

  • ShipmentItem类的Options字段JSON中是null,如果允许null,建议改为string?(C# 8.0+),避免空引用问题。
  • ShipDate字段JSON中是"2023-04-10",DateTimeOffset可以解析这种格式,若出现解析异常,可添加[JsonConverter(typeof(IsoDateTimeConverter))]指定格式。

完成以上修改后,重新执行反序列化即可解决问题。

内容的提问来源于stack exchange,提问作者user1704203

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 03:37:01