-2

以下是 json 格式的数据。

{
    "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
                        }
                    ]
                }
            ]
        }
    ]
}

我试图以列表类型获取这些数据,我得到的错误是这样的!

要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型以使其正常。

我请求您帮助的主题是 json 中的数据,以及 Order 下的 Items 列表。

我尝试了以下两个选项,结果没有改变。

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

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

如果您能在这个话题上提供帮助,我将非常高兴。

谢谢你。

4

2 回答 2

0

响应内容是 JObject 而不是 JArray。

如果您尝试从响应内容中反序列化Order列表,您应该首先创建一个像这样的类:

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

稍后再打电话:

var model = JsonConvert.DeserializeObject <OrderWrapper> (response.Content);
于 2021-01-04T16:40:02.370 回答
0

你的 JSON 是一个对象......而不是一个数组。它是 RootObject 类型的对象,应按以下方式反序列化,

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# 标准应遵循大写属性名称。您可以使用 JsonProperty 确保 JSON 正确映射到您的Order类中的公共属性。

于 2021-01-04T16:40:23.103 回答