How to convert this woocommerce json format to list in C#?

enter image description here [ { "id": 2532, "parent_id": 0, "number": "2532", "order_key": "wc_order_FgqugoSiTCm8C", "created_via": "checkout", "version": "3.8.1", "status": "completed", "currency": "SGD", "date_created": "2020-05-22T07:03:19", "date_created_gmt": "2020-05-22T07:03:19", "date_modified": "2020-05-22T07:06:40", "date_modified_gmt": "2020-05-22T07:06:40", "discount_total": "0.00", "discount_tax": "0.00", "shipping_total": "42.00", "shipping_tax": "0.00", "cart_tax": "0.00", "total": "47.50", "total_tax": "0.00", "prices_include_tax": false, "customer_id": 1, "customer_ip_address": "157.46.19.210", "customer_user_agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36", "customer_note": "", "billing": { "first_name": "Sala", "last_name": "Printer Test", "company": "", "address_1": "sdfgsdf", "address_2": "", "city": "", "state": "", "postcode": "476456", "country": "SG", "email": "ecommerce@unipro.com.sg", "phone": "5674645" }, "shipping": { "first_name": "Sala", "last_name": "Printer Test", "company": "", "address_1": "sdfgsdf", "address_2": "", "city": "", "state": "", "postcode": "476456", "country": "SG" }, "payment_method": "cod", "payment_method_title": "Cash on delivery", "transaction_id": "", "date_paid": "2020-05-22T07:06:40", "date_paid_gmt": "2020-05-22T07:06:40", "date_completed": "2020-05-22T07:06:40", "date_completed_gmt": "2020-05-22T07:06:40", "cart_hash": "4ebf26dee924ed591c6160c0f21d7fe4", "meta_data": [ { "id": 42356, "key": "is_vat_exempt", "value": "no" } ], "line_items": [ { "id": 17, "name": "COCONUT OIL KERAL", "product_id": 2511, "variation_id": 0, "quantity": 1, "tax_class": "", "subtotal": "4.50", "subtotal_tax": "0.00", "total": "4.50", "total_tax": "0.00", "taxes": [], "meta_data": [], "sku": "106096", "price": 4.5 }, { "id": 18, "name": "STAR", "product_id": 2512, "variation_id": 0, "quantity": 1, "tax_class": "", "subtotal": "1.00", "subtotal_tax": "0.00", "total": "1.00", "total_tax": "0.00", "taxes": [], "meta_data": [], "sku": "106081", "price": 1 } ], "tax_lines": [], "shipping_lines": [ { "id": 19, "method_title": "Flat rate", "method_id": "flat_rate", "instance_id": "20", "total": "42.00", "total_tax": "0.00", "taxes": [], "meta_data": [ { "id": 155, "key": "Items", "value": "COCONUT OIL KERAL × 1, STAR × 1" } ] } ], "fee_lines": [], "coupon_lines": [], "refunds": [], "currency_symbol": "$", "_links": { "self": [ { "href": "https://uniprogroups.com/collegecollection/wp-json/wc/v2/orders/2532" } ], "collection": [ { "href": "https://uniprogroups.com/collegecollection/wp-json/wc/v2/orders" } ], "customer": [ { "href": "https://uniprogroups.com/collegecollection/wp-json/wc/v2/customers/1" } ] } }, { "id": 2527, "parent_id": 0, "number": "2527", "order_key": "wc_order_ru3ymWKbxuaib", "created_via": "checkout", "version": "3.8.1", "status": "processing", "currency": "SGD", "date_created": "2020-05-22T04:32:25", "date_created_gmt": "2020-05-22T04:32:25", "date_modified": "2020-05-22T04:32:25", "date_modified_gmt": "2020-05-22T04:32:25", "discount_total": "0.00", "discount_tax": "0.00", "shipping_total": "42.00", "shipping_tax": "0.00", "cart_tax": "0.00", "total": "44.50", "total_tax": "0.00", "prices_include_tax": false, "customer_id": 1, "customer_ip_address": "157.46.19.210", "customer_user_agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36", "customer_note": "", "billing": { "first_name": "Sala", "last_name": "Printer Test", "company": "", "address_1": "sdfgsdf", "address_2": "", "city": "", "state": "", "postcode": "476456", "country": "SG", "email": "ecommerce@unipro.com.sg", "phone": "5674645" }, "shipping": { "first_name": "Sala", "last_name": "Printer Test", "company": "", "address_1": "sdfgsdf", "address_2": "", "city": "", "state": "", "postcode": "476456", "country": "SG" }, "payment_method": "cod", "payment_method_title": "Cash on delivery", "transaction_id": "", "date_paid": null, "date_paid_gmt": null, "date_completed": null, "date_completed_gmt": null, "cart_hash": "932d91bdbf989a4c8c3ea868474dfed8", "meta_data": [ { "id": 42316, "key": "is_vat_exempt", "value": "no" } ], "line_items": [ { "id": 14, "name": "AMBI 1100", "product_id": 2514, "variation_id": 0, "quantity": 1, "tax_class": "", "subtotal": "0.00", "subtotal_tax": "0.00", "total": "0.00", "total_tax": "0.00", "taxes": [], "meta_data": [], "sku": "106015", "price": 0 }, { "id": 15, "name": "GRB GASOGOLLA", "product_id": 2515, "variation_id": 0, "quantity": 1, "tax_class": "", "subtotal": "2.50", "subtotal_tax": "0.00", "total": "2.50", "total_tax": "0.00", "taxes": [], "meta_data": [], "sku": "106014", "price": 2.5 } ], "tax_lines": [], "shipping_lines": [ { "id": 16, "method_title": "Flat rate", "method_id": "flat_rate", "instance_id": "20", "total": "42.00", "total_tax": "0.00", "taxes": [], "meta_data": [ { "id": 131, "key": "Items", "value": "AMBI 1100 × 1, GRB GASOGOLLA × 1" } ] } ], "fee_lines": [], "coupon_lines": [], "refunds": [], "currency_symbol": "$", "_links": { "self": [ { "href": "https://uniprogroups.com/collegecollection/wp-json/wc/v2/orders/2527" } ], "collection": [ { "href": "https://uniprogroups.com/collegecollection/wp-json/wc/v2/orders" } ], "customer": [ { "href": "https://uniprogroups.com/collegecollection/wp-json/wc/v2/customers/1" } ] } } ]

1 answer

  • answered 2020-05-23 11:46 Christoph Lütjen

    Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type '_Model.orderClass+oderlist' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array.

    You're trying to deserialize a list of objects to a single object. Modify your deserialize call, it should look similar to this example

    JsonConvert.DeserializeObject<List<Customer>>(json);