无法反序列化当前的JSON对象 - Null List

时间:2014-10-31 14:50:38

标签: c# json

我在C#中使用Newtonsoft.JSON,我有这样的JSON:

http://woothemes.github.io/woocommerce/rest-api/#get-orders

注意:

coupon_lines []

当我序列化JSON时,我收到此错误:

    List<Order> result = JObject.Parse(GetJson(url))["orders"].ToObject<List<Order>>();

这是优惠券json:

"coupon_lines":{"id":65,"code":"alank10","amount":"33.08"}

无法将当前JSON对象(例如{&#34; name&#34;:&#34; value&#34;})反序列化为类型System.Collections.Generic.List`1 [WooCommerce.Core .Models.CouponLine]&#39;因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。

如何将coupon_lines序列化为列表

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

2 个答案:

答案 0 :(得分:1)

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

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

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

示例中的字符串

"coupon_lines":{"id":65,"code":"alank10","amount":"33.08"} 

是无效的JSON:要使其有效,请在开头添加{,在结尾添加}

{"coupon_lines":{"id":65,"code":"alank10","amount":"33.08"}} 

另外,您说,您想要反序列化列表,并且您的示例中没有列表。要获取列表,您的JSON对象应包含方形括号[]中的对象列表(即使有一个对象):

{  
   "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"
      //}
   ]
}

答案 1 :(得分:0)

我遇到了类似的问题(也有类似的订单,线路,发货等问题......)

首先是: 在你的json中,你可以收到你的列表

    "line_items" : null

没有任何问题

或者也是这个:

    "line_items" : []

诀窍。

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

按要求装饰您的财产
    [JsonProperty(Required = Required.AllowNull)]
    public List<LineItem> line_items { get; set; }

它对我来说很完美。