C# RestSharp API returns data with no Headers

Viewed 25

I am struggling with converting JSON (obtained from a Rest API) into a C# Class. The reason that I am having difficulty is because the JSON data being returned does not appear to have any headers, which directly plays at odds with the generated C# class.

My process is as follows:

  1. Make the call to Rest API and get JSON Data
  2. In Visual Studio, go to Edit > Paste Special > Paste as JSON Class
  3. This sets up the class structure, using parental objects like "Rootobject" and "Class1"
  4. Attempt to deserialize/convert the data into the class by using System.Text.JSON's Deserialize call.

The Deserialize call is failing because the data does not have "Class1" as a header.

Conversely, if I add it manually, the class conversion succeeds:

JSON With Header

The ultimate question, is how do I get the Rest API to return the data while using these types of headers?

If someone could point me in the right direction I would appreciate it.


Edit: Here is the JSON and C# Generated Class -

JSON (This is a single piece of data, but the real JSON has many of these separated by comma)

[
    {
        "id": 63062,
        "parent_id": 0,
        "status": "processing",
        "currency": "USD",
        "version": "6.5.1",
        "prices_include_tax": false,
        "date_created": "",
        "date_modified": "",
        "discount_total": "0.00",
        "discount_tax": "0.00",
        "shipping_total": "5.99",
        "shipping_tax": "0.00",
        "cart_tax": "0.00",
        "total": "18.98",
        "total_tax": "0.00",
        "customer_id": ,
        "order_key": "",
        "billing": {
            "first_name": "",
            "last_name": "",
            "company": "",
            "address_1": "",
            "address_2": "",
            "city": "",
            "state": "",
            "postcode": "",
            "country": "",
            "email": "@.com",
            "phone": ""
        },
        "shipping": {
            "first_name": "",
            "last_name": "",
            "company": "",
            "address_1": "",
            "address_2": "",
            "city": "",
            "state": "",
            "postcode": "",
            "country": "",
            "phone": ""
        },
        "payment_method": "",
        "payment_method_title": "",
        "transaction_id": "",
        "customer_ip_address": "",
        "customer_user_agent": "",
        "created_via": "",
        "customer_note": "",
        "date_completed": null,
        "date_paid": "",
        "cart_hash": "",
        "number": "",
        "meta_data": [
            {
                "id": ,
                "key": "",
                "value": "no"
            },
            {
                "id": ,
                "key": "",
                "value": ""
            },
            {
                "id": ,
                "key": "",
                "value": "0"
            },
            {
                "id": ,
                "key": "",
                "value": ""
            },
            {
                "id": ,
                "key": "",
                "value": "4"
            },
            {
                "id": ,
                "key": "",
                "value": ""
            },
            {
                "id": ,
                "key": "",
                "value": ""
            },
            {
                "id": ,
                "key": "_",
                "value": ""
            },
            {
                "id": ,
                "key": "",
                "value": "18"
            },
            {
                "id": ,
                "key": "_",
                "value": ""
            },
            {
                "id": ,
                "key": "_",
                "value": "yes"
            },
            {
                "id": ,
                "key": "_",
                "value": "23-01"
            },
            {
                "id": ,
                "key": "_",
                "value": ""
            },
            {
                "id": ,
                "key": "",
                "value": "true"
            }
        ],
        "line_items": [
            {
                "id": ,
                "name": "",
                "product_id": ,
                "variation_id": ,
                "quantity": 3,
                "tax_class": "",
                "subtotal": "12.99",
                "subtotal_tax": "0.00",
                "total": "12.99",
                "total_tax": "0.00",
                "taxes": [],
                "meta_data": [
                    {
                        "id": ,
                        "key": "",
                        "value": "medium_jar",
                        "display_key": "Size",
                        "display_value": ""
                    }
                ],
                "sku": "",
                "price": 4.33,
                "parent_name": ""
            }
        ],
        "tax_lines": [],
        "shipping_lines": [
            {
                "id": ,
                "method_title": "",
                "method_id": "",
                "instance_id": "0",
                "total": "5.99",
                "total_tax": "0.00",
                "taxes": [],
                "meta_data": []
            }
        ],
        "fee_lines": [],
        "coupon_lines": [],
        "refunds": [],
        "payment_url": "https:///",
        "date_created_gmt": "",
        "date_modified_gmt": "",
        "date_completed_gmt": null,
        "date_paid_gmt": "",
        "currency_symbol": "$",
        "_links": {
            "self": [
                {
                    "href": "https:///wp-json/wc"
                }
            ],
            "collection": [
                {
                    "href": "https:///wp-jso"
                }
            ],
            "customer": [
                {
                    "href": "https:///wp-json/w"
                }
            ]
        }
    }
]

C# CODE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{

    public class Rootobject
    {
        public Class1[] Class1 { get; set; }
    }

    public class Class1
    {
        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 DateTime date_paid { get; set; }
        public string cart_hash { get; set; }
        public string number { get; set; }
        public Meta_Data[] meta_data { get; set; }
        public Line_Items[] line_items { get; set; }
        public object[] tax_lines { get; set; }
        public Shipping_Lines[] shipping_lines { get; set; }
        public object[] fee_lines { get; set; }
        public object[] coupon_lines { get; set; }
        public object[] refunds { get; set; }
        public string payment_url { get; set; }
        public DateTime date_created_gmt { get; set; }
        public DateTime date_modified_gmt { get; set; }
        public object date_completed_gmt { get; set; }
        public DateTime 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 _Links
    {
        public Self[] self { get; set; }
        public Collection[] collection { get; set; }
    }

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

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

    public class Meta_Data
    {
        public int id { get; set; }
        public string key { get; set; }
        public string value { get; set; }
    }

    public class Line_Items
    {
        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 object[] taxes { get; set; }
        public Meta_Data1[] meta_data { get; set; }
        public string sku { get; set; }
        public float price { get; set; }
        public string parent_name { get; set; }
    }

    public class Meta_Data1
    {
        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 Shipping_Lines
    {
        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 object[] taxes { get; set; }
        public object[] meta_data { get; set; }
    }

}

0 Answers
Related