-2

Below is data in a json format.

{
    "count": 1,
    "orders": [
        {
            "instructions": "",
            "coupons": [],
            "tax_list": [
                {
                    "type": "item",
                    "value": 1.83,
                    "rate": 0.18
                }
            ],
            "missed_reason": null,
            "billing_details": null,
            "fulfillment_option": null,
            "id": 213102806,
            "total_price": 12,
            "sub_total_price": 12,
            "tax_value": 1.83,
            "persons": 0,
            "latitude": "38.43105653776779",
            "longitude": "27.141175570348768",
            "client_first_name": "Sinem1",
            "client_last_name": "Coşkun1",
            "client_email": "sunarferhat851@gmail.com",
            "client_phone": "+905071420000",
            "restaurant_name": "Bobs Döner",
            "currency": "TRY",
            "type": "delivery",
            "status": "pending",
            "source": "website",
            "pin_skipped": false,
            "accepted_at": null,
            "tax_type": "GROSS",
            "tax_name": "KDV",
            "fulfill_at": null,
            "client_language": "tr",
            "integration_payment_provider": null,
            "integration_payment_amount": 0,
            "reference": null,
            "restaurant_id": 2178794,
            "client_id": 199145778,
            "updated_at": "2021-01-04T13:01:22.000Z",
            "restaurant_phone": "+90 232 445 44 44",
            "restaurant_timezone": "Europe/Istanbul",
            "card_type": null,
            "used_payment_methods": [
                "CASH"
            ],
            "company_account_id": 895916,
            "pos_system_id": 30000097,
            "restaurant_key": "qQM9yqtQaVepdJZ",
            "restaurant_country": "Turkey",
            "restaurant_city": "İzmir",
            "restaurant_zipcode": "35210",
            "restaurant_street": "No:1 D:No:3, 35210 Konak/İzmir",
            "restaurant_latitude": "39.42531962962631",
            "restaurant_longitude": "37.13854324236258",
            "client_marketing_consent": true,
            "restaurant_token": "dony",
            "gateway_transaction_id": null,
            "gateway_type": null,
            "api_version": 2,
            "payment": "CASH",
            "for_later": false,
            "client_address": "Caz Mah. 1625 Sk., İzmir",
            "client_address_parts": {
                "street": "Caz Mah. 1625 Sk.",
                "city": "İzmir"
            },
            "items": [
                {
                    "id": 287217709,
                    "name": "Tavuk Dürüm",
                    "total_item_price": 12,
                    "price": 12,
                    "quantity": 1,
                    "instructions": "",
                    "type": "item",
                    "type_id": 6416211,
                    "tax_rate": 0.18,
                    "tax_value": 1.8305,
                    "parent_id": null,
                    "item_discount": 0,
                    "cart_discount_rate": 0,
                    "cart_discount": 0,
                    "tax_type": "GROSS",
                    "options": [
                        {
                            "id": 271793671,
                            "name": "Tavuk Dürüm",
                            "price": 0,
                            "group_name": "Boy",
                            "quantity": 1,
                            "type": "size",
                            "type_id": 3429180
                        }
                    ]
                }
            ]
        }
    ]
}

I'm trying to get this data in list type and the error I got is like this!

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal.

The topic I ask you to help with is the data in json, and the Items list under Order.

I tried the two options below and the result did not change.

var des = (Root) JsonConvert.DeserializeObject (response.Content, typeof (Root));

var model = JsonConvert.DeserializeObject <List <Order>> (response.Content);

I would be very happy if you could help with this topic.

Thank you.

webing
  • 13
  • 4
  • Can you show us your Oder & Root classes? – XavierAM Jan 04 '21 at 16:37
  • The error message you say you receive indicates that some other error message was previously issued. Are you sure that this is the only message and the entire message that you get? – Mike Nakis Jan 04 '21 at 16:37
  • Working example with this json: https://dotnetfiddle.net/YnAPxl. You need to provide more code, this is not a minimal reproducable example –  Jan 04 '21 at 16:45

2 Answers2

0

The response content is a JObject and not a JArray.

If you try to deserialize the Order list from the response content, you should first create a class like this one :

public class OrderWrapper
{
    public int Count { get; set; }
    public List<Order> Orders { get; set; }
} 

and call later on :

var model = JsonConvert.DeserializeObject <OrderWrapper> (response.Content);
XavierAM
  • 1,627
  • 1
  • 14
  • 30
0

Your JSON is an object.. and not an array. It is an object of RootObject type and should be deserialized the following way,

public class Rootobject
{
    [JsonProperty("count")]
    public int Count { get; set; }
    [JsonProperty("orders")]
    public List<Order> Orders { get; set; }
}

public class Order
{
    [JsonProperty("instructions")]
    public string instructions { get; set; }
    [JsonProperty("id")]
    public int Id { get; set; }
    [JsonProperty("total_price")]
    public int TotalPrice { get; set; }
    [JsonProperty("sub_total_price")]
    public int SubTotalPrice { get; set; }
    ... 
}

Rootobject myObject = JsonConvert.DeserializeObject<Rootobject>(response.Content);

Console.WriteLine(myObject.Count);
// should print 1 based on your json.

List<Order> orders = myObject.Orders;

C# standards should be followed with Uppercase property names. You can use JsonProperty to ensure JSON maps correctly to the public properties in your Order class.

Jawad
  • 11,028
  • 3
  • 24
  • 37