C# 无法反序列化当前JSON对象-空列表

C# 无法反序列化当前JSON对象-空列表,c#,json,C#,Json,我在C#中使用Newtonsoft.JSON,我的JSON如下: 注: 当我序列化JSON时,会出现以下错误: List<Order> result = JObject.Parse(GetJson(url))["orders"].ToObject<List<Order>>(); 无法将当前JSON对象(例如{“名称”:“值”})反序列化为类型“System.Collections.Generic.List`1[WooCommerce.Core.Mo

我在C#中使用Newtonsoft.JSON,我的JSON如下:

注:

当我序列化JSON时,会出现以下错误:

    List<Order> result = JObject.Parse(GetJson(url))["orders"].ToObject<List<Order>>();
无法将当前JSON对象(例如{“名称”:“值”})反序列化为类型“System.Collections.Generic.List`1[WooCommerce.Core.Models.CouponLine]”,因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化

如何序列化要列出的优惠券行

public class CouponLine
{
    public int id { get; set; }
    public string code { get; set; }
    public decimal amount { get; set; }
}
这是我的班级:

    public class CouponLines
{
    public List<CouponLine> lines { get; set; }
}

public class Order
{
    public int id { get; set; }
    public string order_number { get; set; }
    public DateTime created_at { get; set; }
    public DateTime updated_at { get; set; }
    public DateTime completed_at { get; set; }
    public string status { get; set; }
    public string currency { get; set; }
    public decimal total { get; set; }
    public decimal subtotal { get; set; }
    public int total_line_items_quantity { get; set; }
    public decimal total_tax { get; set; }
    public decimal total_shipping { get; set; }
    public decimal cart_tax { get; set; }
    public decimal shipping_tax { get; set; }
    public decimal total_discount { get; set; }
    public decimal cart_discount { get; set; }
    public decimal order_discount { get; set; }
    public string shipping_methods { get; set; }
    public PaymentDetails payment_details { get; set; }
    public BillingAddress billing_address { get; set; }
    public ShippingAddress shipping_address { get; set; }
    public string note { get; set; }
    public string customer_ip { get; set; }
    public string customer_user_agent { get; set; }
    public string customer_id { get; set; }
    public string view_order_url { get; set; }
    public List<LineItem> line_items { get; set; }
    public List<ShippingLine> shipping_lines { get; set; }
    //public List<object> tax_lines { get; set; }
    //public List<object> fee_lines { get; set; }
    public CouponLines coupon_lines { get; set; }
    public Customer customer { get; set; }
}
公共类耦合线
{
公共列表行{get;set;}
}
公共阶级秩序
{
公共int id{get;set;}
公共字符串顺序_编号{get;set;}
在{get;set;}处创建的公共日期时间
公共日期时间在{get;set;}更新
公共日期时间在{get;set;}处完成
公共字符串状态{get;set;}
公共字符串货币{get;set;}
公共十进制总数{get;set;}
公共十进制小计{get;set;}
公共整数总计\行\项\数量{get;set;}
公共十进制总税{get;set;}
公共十进制总数{get;set;}
公共十进制cart_tax{get;set;}
公共十进制装运税{get;set;}
公共十进制总折扣{get;set;}
公共十进制购物车折扣{get;set;}
公共十进制顺序\u折扣{get;set;}
公共字符串shipping_方法{get;set;}
公共付款详细信息付款详细信息{get;set;}
公共计费地址计费地址{get;set;}
公共ShippingAddress shipping_地址{get;set;}
公共字符串注释{get;set;}
公共字符串customer_ip{get;set;}
公共字符串customer\u user\u agent{get;set;}
公共字符串customer_id{get;set;}
公共字符串视图\u顺序\u url{get;set;}
公共列表行_项{get;set;}
公共列表传送线{get;set;}
//公共列表税_行{get;set;}
//公共列表费_行{get;set;}
公共耦合线优惠券线{get;set;}
公共客户客户{get;set;}
}

您应该使用某种包含此列表的“根”对象,例如:

var parsedResponse = JsonConvert.DeserializeObject<CouponLines>(jsonResponse);
//...

public class CouponLines
{
    public List<CouponLine> order { get; set; }
}
JSON无效:要使其有效,请在开头添加
{
,在结尾添加
}

{"coupon_lines":{"id":65,"code":"alank10","amount":"33.08"}} 
另外,您说过,您希望反序列化列表,但在您的示例中并没有列表。要获取列表,您的JSON对象应该包含方形breckets中的对象列表(即使有一个对象):

{  
   "coupon_lines":[  
      {  
         "id":65,
         "code":"alank10",
         "amount":"33.08"
      }
      //if you have several objects - put it here after comma:
      //,
      //{  
      //   "id":100500,
      //   "code":"somecode",
      //   "amount":"33.08"
      //}
   ]
}
我也有类似的问题(订单、生产线、装运等方面也有类似的问题)

第一件事是: 在json中,您可以通过以下方式接收列表:

    "line_items" : null
没有任何问题

或者这个:

    "line_items" : []
这就是诀窍

但是如果您想要完美的解决方案,那么您可以避免完全发送line_items参数,解决方案非常简单, 只要按要求用

    [JsonProperty(Required = Required.AllowNull)]
    public List<LineItem> line_items { get; set; }
[JsonProperty(Required=Required.AllowNull)]
公共列表行_项{get;set;}

它对我来说非常有效。

您能显示输入json吗?而
Order
类呢?我在最近的编辑中尝试了这一点,但是,我仍然得到一个错误,即JSON是lsit而不是对象。我做得对吗?在你发布测试数据示例后,我更新了答案
    "line_items" : null
    "line_items" : []
    [JsonProperty(Required = Required.AllowNull)]
    public List<LineItem> line_items { get; set; }