[英]Cannot deserialize the current JSON object - Null List
我在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對象(例如{“ name”:“ value”})為類型'System.Collections.Generic.List`1 [WooCommerce.Core.Models.CouponLine]',因為該類型需要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; }
}
您應該使用包含此列表的某種“根”對象,例如:
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"
//}
]
}
我遇到了類似的問題(訂單,訂單行,貨運等也有類似的要求)
第一件事是:在您的json中,您可以將列表接收為
"line_items" : null
沒有任何問題
還是這個:
"line_items" : []
絕招。
但是如果您想要一個完美的解決方案,那么可以避免完全發送line_items參數,該解決方案非常簡單,只需使用請求裝飾您的屬性即可
[JsonProperty(Required = Required.AllowNull)]
public List<LineItem> line_items { get; set; }
對我來說效果很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.