Aquí están mis modelos:

public class Order
{
    public IEnumerable<LineItem> LineItems { get; set; }
}
public class LineItem
{
    [JsonPropertyName("id")]
    public int Id { get; set; }
    [JsonPropertyName("name")]
    public string Name { get; set; }
    [JsonPropertyName("product_id")]
    public int ProductId { get; set; }
    [JsonPropertyName("variation_id")]
    public int VariationId { get; set; }
    [JsonPropertyName("quantity")]
    public int Quantity { get; set; }
    [JsonPropertyName("tax_class")]
    public string TaxClass { get; set; }
    [JsonPropertyName("subtotal")]
    public string Subtotal { get; set; }
    [JsonPropertyName("subtotal_tax")]
    public string SubtotalTax { get; set; }
    [JsonPropertyName("total")]
    public string Total { get; set; }
    [JsonPropertyName("total_tax")]
    public string TotalTax { get; set; }
    [JsonPropertyName("taxes")]
    public List<object> Taxes { get; set; }
    [JsonPropertyName("meta_data")]
    public List<MetaData> MetaData { get; set; }
    [JsonPropertyName("sku")]
    public string Sku { get; set; }
    [JsonPropertyName("price")]
    public double Price { get; set; }
    [JsonPropertyName("parent_name")]
    public string ParentName { get; set; }
}

Como puede ver, la propiedad HttpRequest contiene una matriz de elementos de línea:


[
    {
    "id":7928,
    "parent_id":0,
    "status":"on-hold",
    "currency":"GBP",
    "version":"5.7.1",
    "prices_include_tax":true,
    "date_created":"2021-10-04T00:26:22",
    "date_modified":"2021-10-04T00:26:22",
    "discount_total":"0.00",
    "discount_tax":"0.00",
    "shipping_total":"0.00",
    "shipping_tax":"0.00",
    "cart_tax":"0.00",
    "total":"2.50",
    "total_tax":"0.00",
    "customer_id":3,
    "order_key":"redacted",
    "billing":{
       "first_name":"redacted",
       "last_name":"redacted",
       "company":"",
       "address_1":"redacted",
       "address_2":"redacted",
       "city":"redacted",
       "state":"redacted",
       "postcode":"redacted",
       "country":"GB",
       "email":"redacted",
       "phone":"redacted"
    },
    "shipping":{
        "first_name":"redacted",
        "last_name":"redacted",
        "company":"",
        "address_1":"redacted",
        "address_2":"redacted Lane",
        "city":"redacted",
        "state":"redacted",
        "postcode":"redacted",
        "country":"GB",
        "phone":""
     },
     "payment_method":"bacs",
     "payment_method_title":"Direct bank transfer",
     "transaction_id":"",
     "customer_ip_address":"redacted",
     "customer_user_agent":"redacted",
     "created_via":"redacted",
     "customer_note":"",
     "date_completed":null,
     "date_paid":null,
     "cart_hash":"redacted",
     "number":"redacted",
     "meta_data":[
        {
           "id":190011,
           "key":"redacted",
           "value":"no"
        },
        {
           "id":190012,
           "key":"redacted",
           "value":"yes"
        },
        {
           "id":190016,
           "key":"_new_order_email_sent",
           "value":"true"
        },
        {
           "id":190017,
           "key":"_thankyou_action_done",
           "value":"1"
        }
     ],
     "line_items":[
        {
           "id":5287,
           "name":"John Guest 3\/8 to 1\/4 Fitting PI0112f4S",
           "product_id":5699,
           "variation_id":0,
           "quantity":1,
           "tax_class":"",
           "subtotal":"2.50",
           "subtotal_tax":"0.00",
           "total":"2.50",
           "total_tax":"0.00",
           "taxes":[
              
           ],
           "meta_data":[
              {
                 "id":48648,
                 "key":"_WCPA_order_meta_data",
                 "value":"",
                 "display_key":"_WCPA_order_meta_data",
                 "display_value":""
              }
           ],
           "sku":"",
           "price":2.5,
           "parent_name":null
        }
     ],
     "tax_lines":[
        
     ],
     "shipping_lines":[
        {
           "id":5288,
           "method_title":"Collect from store",
           "method_id":"local_pickup",
           "instance_id":"13",
           "total":"0.00",
           "total_tax":"0.00",
           "taxes":[
              
           ],
           "meta_data":[
              {
                 "id":48647,
                 "key":"Items",
                 "value":"John Guest 3\/8 to 1\/4 Fitting PI0112f4S &times; 1",
                 "display_key":"Items",
                 "display_value":"John Guest 3\/8 to 1\/4 Fitting PI0112f4S &times; 1"
              }
           ]
        }
     ],
     "fee_lines":[
        
     ],
     "coupon_lines":[
        
     ],
     "refunds":[
        
     ],
     "date_created_gmt":"2021-10-03T23:26:22",
     "date_modified_gmt":"2021-10-03T23:26:22",
     "date_completed_gmt":null,
     "date_paid_gmt":null,
     "currency_symbol":"\u00a3",
     "_links":{
        "self":[
           {
              "href":"redacted"
           }
        ],
        "collection":[
           {
              "href":"redacted"
           }
        ],
        "customer":[
           {
              "href":"redacted"
           }
        ]
     }
  },
]


Me pregunto por qué cuando esto se deserializa usando

public class WooCommerceOrders
    {
        private HttpRequestService _httpRequestService;
        private string _url;

        public WooCommerceOrders()
        {
            _httpRequestService = new HttpRequestService();
            _url = "https://kegthat.com/wp-json/wc/v3/orders";
        }

        public List<Order> GetOrdersJson()
        {
            var result = _httpRequestService.CallApi(_url).Content.ReadAsStringAsync();
            return _httpRequestService.DeserializeApiResponseJson<List<Order>>(result.Result);
        }
    }

Devuelve el json para artículos de línea

"lineItems": null,

Sin embargo, algunos campos como devolución de envío

"shipping": {
"firstName": null,
"lastName": null,
"company": "",
"address1": null,
"address2": null,
"city": "redacting city",
"state": "Cheshire",
"postcode": "redacting postcode",
"country": "GB",
"phone": ""
},

Esto no me permitirá agregar mucho más código que contenido Esto no me permitirá agregar mucho más código que contenido Esto no me permitirá agregar mucho más código que contenido Esto no me permitirá agregar mucho más código que contenido Esto no me permitirá agregar demasiado mucho más código que contenido Esto no me permitirá agregar mucho más código que contenido Esto no me permitirá agregar mucho más código que contenido Esto no me permitirá agregar mucho más código que contenido Esto no me permitirá agregar mucho más código que contenido

¿Por qué no puedo devolver artículos de línea?

c#
0
Ben 4 oct. 2021 a las 20:49

3 respuestas

La mejor respuesta

Pruebe esto usando Newtonsoft.Json

using Newtonsoft.Json;
.....

 public List<Order> GetOrdersJson()
{
var result = _httpRequestService.CallApi(_url).Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject< List<Order>>(result.Result);
}

Prueba para obtener line_items

var result= GetOrdersJson();
var lineItems=result[0].line_items;

[
  {
    "id": 5287,
    "name": "John Guest 3/8 to 1/4 Fitting PI0112f4S",
    "product_id": 5699,
    "variation_id": 0,
    "quantity": 1,
    "tax_class": "",
    "subtotal": "2.50",
    "subtotal_tax": "0.00",
    "total": "2.50",
    "total_tax": "0.00",
    "taxes": [],
    "meta_data": [
      {
        "id": 48648,
        "key": "_WCPA_order_meta_data",
        "value": "",
        "display_key": "_WCPA_order_meta_data",
        "display_value": ""
      }
    ],
    "sku": "",
    "price": 2.5,
    "parent_name": null
  }
]

Clases

public class Order
{
    public int id { get; set; }
    public int parent_id { get; set; }
    public string status { get; set; }
    public string currency { get; set; }
    public string version { get; set; }
    public bool prices_include_tax { get; set; }
    public DateTime date_created { get; set; }
    public DateTime date_modified { get; set; }
    public string discount_total { get; set; }
    public string discount_tax { get; set; }
    public string shipping_total { get; set; }
    public string shipping_tax { get; set; }
    public string cart_tax { get; set; }
    public string total { get; set; }
    public string total_tax { get; set; }
    public int customer_id { get; set; }
    public string order_key { get; set; }
    public Billing billing { get; set; }
    public Shipping shipping { get; set; }
    public string payment_method { get; set; }
    public string payment_method_title { get; set; }
    public string transaction_id { get; set; }
    public string customer_ip_address { get; set; }
    public string customer_user_agent { get; set; }
    public string created_via { get; set; }
    public string customer_note { get; set; }
    public object date_completed { get; set; }
    public object date_paid { get; set; }
    public string cart_hash { get; set; }
    public string number { get; set; }
    public List<MetaData> meta_data { get; set; }
    public List<LineItem> line_items { get; set; }
    public List<object> tax_lines { get; set; }
    public List<ShippingLine> shipping_lines { get; set; }
    public List<object> fee_lines { get; set; }
    public List<object> coupon_lines { get; set; }
    public List<object> refunds { get; set; }
    public DateTime date_created_gmt { get; set; }
    public DateTime date_modified_gmt { get; set; }
    public object date_completed_gmt { get; set; }
    public object date_paid_gmt { get; set; }
    public string currency_symbol { get; set; }
    public Links _links { get; set; }
}


public class Billing
{
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string company { get; set; }
    public string address_1 { get; set; }
    public string address_2 { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string postcode { get; set; }
    public string country { get; set; }
    public string email { get; set; }
    public string phone { get; set; }
}

public class Shipping
{
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string company { get; set; }
    public string address_1 { get; set; }
    public string address_2 { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string postcode { get; set; }
    public string country { get; set; }
    public string phone { get; set; }
}

public class MetaData
{
    public int id { get; set; }
    public string key { get; set; }
    public string value { get; set; }
    public string display_key { get; set; }
    public string display_value { get; set; }
}

public class LineItem
{
    public int id { get; set; }
    public string name { get; set; }
    public int product_id { get; set; }
    public int variation_id { get; set; }
    public int quantity { get; set; }
    public string tax_class { get; set; }
    public string subtotal { get; set; }
    public string subtotal_tax { get; set; }
    public string total { get; set; }
    public string total_tax { get; set; }
    public List<object> taxes { get; set; }
    public List<MetaData> meta_data { get; set; }
    public string sku { get; set; }
    public double price { get; set; }
    public object parent_name { get; set; }
}

public class ShippingLine
{
    public int id { get; set; }
    public string method_title { get; set; }
    public string method_id { get; set; }
    public string instance_id { get; set; }
    public string total { get; set; }
    public string total_tax { get; set; }
    public List<object> taxes { get; set; }
    public List<MetaData> meta_data { get; set; }
}

public class Self
{
    public string href { get; set; }
}

public class Collection
{
    public string href { get; set; }
}

public class Customer
{
    public string href { get; set; }
}

public class Links
{
    public List<Self> self { get; set; }
    public List<Collection> collection { get; set; }
    public List<Customer> customer { get; set; }
}

0
Serge 4 oct. 2021 a las 18:44

IEnumerable<T> no es un tipo válido para el serializador. No define ninguna implementación concreta de una colección para que el serializador analice el JSON. Utilice List<T> al deserializar matrices JSON.

0
Andrew H 4 oct. 2021 a las 18:10
 public List<Order> GetOrdersJson()
    {
        var result = _httpRequestService.CallApi(_url).Content.ReadAsStringAsync();
        return _httpRequestService.DeserializeApiResponseJson<Order>(result.Result);
    }  

Deserializador List<Order> que no es el Deserializador que está esperando. Prueba return _httpRequestService.DeserializeApiResponseJson<Order> en su lugar

0
jmag 4 oct. 2021 a las 18:44