0

I am using Newtonsoft.JSON in C# and I have JSON like this:

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

Note:

coupon_lines []

When I serialize the JSON, I get this error:

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

Here is the coupon json:

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

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[WooCommerce.Core.Models.CouponLine]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

How do I serialize coupon_lines to List

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; }
}
1
  • 1
    Can you show the input json? And the Order class? Commented Oct 31, 2014 at 14:52

2 Answers 2

1

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:

{"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 []:

{  
   "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"
      //}
   ]
}
Sign up to request clarification or add additional context in comments.

2 Comments

I tried that in my latest edit, however, I still get an error that the JSON is a lsit an not an object. Did I do it right?
I've updated the answer after you've post example of your test data
0

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

    "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

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

it worked perfectly for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.