简体   繁体   English

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

[英]Cannot deserialize the current JSON object - Null List

I am using Newtonsoft.JSON in C# and I have JSON like this: 我在C#中使用Newtonsoft.JSON,并且具有这样的JSON:

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

Note: 注意:

coupon_lines []

When I serialize the JSON, I get this error: 序列化JSON时,出现以下错误:

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

Here is the coupon json: 这是优惠券json:

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

Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List`1[WooCommerce.Core.Models.CouponLine]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. 无法反序列化当前JSON对象(例如{“ name”:“ value”})为类型'System.Collections.Generic.List`1 [WooCommerce.Core.Models.CouponLine]',因为该类型需要JSON数组(例如[ 1,2,3])正确反序列化。

How do I serialize coupon_lines to List 如何将coupon_lines序列化为列表

public class CouponLine
{
    public int id { get; set; }
    public string code { get; set; }
    public decimal amount { get; set; }
}

Here is my class: 这是我的课:

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

You should use some kind of "root" object that contains this list, for example: 您应该使用包含此列表的某种“根”对象,例如:

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

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

The string from your example 您示例中的字符串

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

is invalid JSON: to make it valid add { at the beginning and } at the end: 是无效的JSON:要使其有效,请在开头添加{ ,在末尾添加}

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

Also, you said, you want to deserialize list, and you do not have list in your example. 另外,您说过,您想对列表进行反序列化,并且您的示例中没有列表。 To get list your JSON object should contain list of objects (even if there is one object) in square breckets [] : 要获取列表,您的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"
      //}
   ]
}

I had a similar problem (and also similar reuqest with orders, lines, shippings etc...) 我遇到了类似的问题(订单,订单行,货运等也有类似的要求)

first thing is that: in your json, you can receive your list as 第一件事是:在您的json中,您可以将列表接收为

    "line_items" : null

without any problems 没有任何问题

or also this one: 还是这个:

    "line_items" : []

does the trick. 绝招。

but if you want the perfect solution, so you can avoid to send the line_items parameter completely, the solution is pretty easy, just decorate your property on the request with 但是如果您想要一个完美的解决方案,那么可以避免完全发送line_items参数,该解决方案非常简单,只需使用请求装饰您的属性即可

    [JsonProperty(Required = Required.AllowNull)]
    public List<LineItem> line_items { get; set; }

it worked perfectly for me. 对我来说效果很好。

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

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