Как десериализовать WooCommerce Json для просмотра данных - PullRequest
0 голосов
/ 13 апреля 2020

Я создаю настольное приложение для печати билетов доставки. У меня есть строка Json, и я хочу десериализовать ее в набор данных. И оттуда создайте dagagview и распечатайте билеты. Я новичок в Json, мне нужен кто-то, чтобы начать меня с примером кода Мне, вероятно, нужно использовать классы, но это также для меня ново.

string JsonString = @"
[ 
    { 
        "id":704,
        "parent_id":0,
        "number":"704",
        "order_key":"wc_order_XJ",
        "created_via":"checkout",
        "version":"4.0.1",
        "status":"completed",
        "currency":"EUR",
        "date_created":"2020-04-09T10:47:22",
        "date_created_gmt":"2020-04-09T08:47:22",
        "date_modified":"2020-04-09T11:41:36",
        "date_modified_gmt":"2020-04-09T09:41:36",
        "discount_total":"0.00",
        "discount_tax":"0.00",
        "shipping_total":"0.00",
        "shipping_tax":"0.00",
        "cart_tax":"0.00",
        "total":"55.50",
        "total_tax":"0.00",
        "prices_include_tax":false,
        "customer_id":0,
        "customer_ip_address":"00",
        "customer_user_agent":"Mozilla",
        "customer_note":"Mijn vader",
        "billing":{ 
            "first_name":"K",
            "last_name":"van",
            "company":"",
            "address_1":"Burg.",
            "address_2":"",
            "city":"B",
            "state":"",
            "postcode":"54",
            "country":"NL",
            "email":"JohnDoe@gmail.com",
            "phone":"06"
        },
        "shipping":{ 
            "first_name":"L",
            "last_name":"S",
            "company":"",
            "address_1":"Water",
            "address_2":"",
            "city":"Venlo",
            "state":"",
            "postcode":"59",
            "country":"NL"
        },
        "payment_method":"cod",
        "payment_method_title":"Pin of tikki",
        "transaction_id":"",
        "date_paid":"2020-04-09T11:41:36",
        "date_paid_gmt":"2020-04-09T09:41:36",
        "date_completed":"2020-04-09T11:41:36",
        "date_completed_gmt":"2020-04-09T09:41:36",
        "cart_hash":"80114d6b",
        "meta_data":[ 
            { 
                "id":5881,
                "key":"_billing_wooccm11",
                "value":"donderdag"
            },
            { 
                "id":5882,
                "key":"_shipping_phone",
                "value":""
            },
            { 
                "id":5883,
                "key":"is_vat_exempt",
                "value":"no"
            },
            { 
                "id":5934,
                "key":"slide_template",
                "value":"default"
            }
        ],
        "line_items":[ 
            { 
                "id":271,
                "name":"Kabeljauwhaas",
                "product_id":496,
                "variation_id":0,
                "quantity":2,
                "tax_class":"",
                "subtotal":"43.00",
                "subtotal_tax":"0.00",
                "total":"43.00",
                "total_tax":"0.00",
                "taxes":[ 
                ],
                "meta_data":[ 
                ],
                "sku":"",
                "price":21.5
            },
            { 
                "id":272,
                "name":"Asperge",
                "product_id":407,
                "variation_id":0,
                "quantity":1,
                "tax_class":"",
                "subtotal":"12.50",
                "subtotal_tax":"0.00",
                "total":"12.50",
                "total_tax":"0.00",
                "taxes":[ 
                ],
                "meta_data":[
                ],
                "sku":"",
                "price":12.5
            }
        ],
        "tax_lines":[ 
        ],
        "shipping_lines":[ 
            { 
                "id":273,
                "method_title":"Gratis verzending",
                "method_id":"free_shipping",
                "instance_id":"2",
                "total":"0.00",
                "total_tax":"0.00",
                "taxes":[ 
                ],
                "meta_data":[ 
                    { 
                        "id":2190,
                        "key":"Items",
                        "value":"Kabeljauwhaas × 1"
                    }
                ]
            }
        ],
        "fee_lines":[ 
        ],
        "coupon_lines":[ 
        ],
        "refunds":[ 
        ],
        "currency_symbol":"\u20ac",
        "_links":{ 
            "self":[
                { 
                    "href":"https:\/\/sample.com\/wp-json\/wc\/v3\/orders\/704"
                }
            ],
            "collection":[ 
                { 
                    "href":"https:\/\/sample.com\/wp-json\/wc\/v3\/orders"
                }
            ]
        }
    },
    { 
        "id":703,
        "parent_id":0,
        "number":"703",
        "order_key":"wc_order_Yz",
        "created_via":"checkout",
        "version":"4.0.1",
        "status":"completed",
        "currency":"EUR",
        "date_created":"2020-04-09T08:41:43",
        "date_created_gmt":"2020-04-09T06:41:43",
        "date_modified":"2020-04-09T09:55:13",
        "date_modified_gmt":"2020-04-09T07:55:13",
        "discount_total":"0.00",
        "discount_tax":"0.00",
        "shipping_total":"0.00",
        "shipping_tax":"0.00",
        "cart_tax":"0.00",
        "total":"12.50",
        "total_tax":"0.00",
        "prices_include_tax":false,
        "customer_id":0,
        "customer_ip_address":"00",
        "customer_user_agent":"Mozilla",
        "customer_note":"Kan ik ",
        "billing":{ 
            "first_name":"K",
            "last_name":"He",
            "company":"",
            "address_1":"Ho",
            "address_2":"",
            "city":"Te",
            "state":"",
            "postcode":"59",
            "country":"NL",
            "email":"johndoe@gmail.com",
            "phone":"00"
        },
        "shipping":{ 
            "first_name":"K",
            "last_name":"He",
            "company":"",
            "address_1":"Ho",
            "address_2":"",
            "city":"Te",
            "state":"",
            "postcode":"59",
            "country":"NL"
        },
        "payment_method":"cod",
        "payment_method_title":"Pin of tikki",
        "transaction_id":"",
        "date_paid":"2020-04-09T09:55:13",
        "date_paid_gmt":"2020-04-09T07:55:13",
        "date_completed":"2020-04-09T09:55:13",
        "date_completed_gmt":"2020-04-09T07:55:13",
        "cart_hash":"c372cb5",
        "meta_data":[ 
            { 
                "id":5816,
                "key":"_billing_wooccm11",
                "value":"vrijdag"
            },
            { 
                "id":5817,
                "key":"_shipping_phone",
                "value":"0616"
            },
            { 
                "id":5818,
                "key":"is_vat_exempt",
                "value":"no"
            }
        ],
        "line_items":[ 
            { 
                "id":269,
                "name":"Asperge",
                "product_id":407,
                "variation_id":0,
                "quantity":1,
                "tax_class":"",
                "subtotal":"12.50",
                "subtotal_tax":"0.00",
                "total":"12.50",
                "total_tax":"0.00",
                "taxes":[ 
                ],
                "meta_data":[ 
                ],
                "sku":"",
                "price":12.5
            }
        ],
        "tax_lines":[ 
        ],
        "shipping_lines":[ 
            { 
                "id":270,
                "method_title":"Gratis verzending",
                "method_id":"free_shipping",
                "instance_id":"2",
                "total":"0.00",
                "total_tax":"0.00",
                "taxes":[ 
                ],
                "meta_data":[ 
                    { 
                        "id":2166,
                        "key":"Items",
                        "value":"Asperge × 1"
                    }
                ]
            }
        ],
        "fee_lines":[ 
        ],
        "coupon_lines":[ 
        ],
        "refunds":[ 
        ],
        "currency_symbol":"\u20ac",
        "_links":{ 
            "self":[ 
                { 
                    "href":"https:\/\/sample.com\/wp-json\/wc\/v3\/orders\/703"
                }
            ],
            "collection":[ 
                { 
                    "href":"https:\/\/sample.com\/wp-json\/wc\/v3\/orders"
                }
            ]
        }
    }
]"

Я попробовал этот пример от newtonsoft: я добавил для этого несколько вещей в jsonstring, чтобы получить таблицу. Это дает мне ошибку: «Неожиданный JSON токен при чтении DataTable: StartObject. Path 'orders [0] .billing'line1, position 419»

JsonString = JsonString "{\"orders\":" + JsonString + "}" 
DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(json);
DataTable dataTable = dataSet.Tables["orders"];

классов, созданных из Json

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 class MetaData
{
    public int id { get; set; }
    public string key { get; set; }
    public string 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<object> meta_data { get; set; }
    public string sku { get; set; }
    public double price { get; set; }
}

public class MetaData2
{
    public int id { get; set; }
    public string key { get; set; }
    public string value { 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<MetaData2> meta_data { get; set; }
}

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

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

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

public class RootObject
{
    public int id { get; set; }
    public int parent_id { get; set; }
    public string number { get; set; }
    public string order_key { get; set; }
    public string created_via { get; set; }
    public string version { get; set; }
    public string status { get; set; }
    public string currency { get; set; }
    public DateTime date_created { get; set; }
    public DateTime date_created_gmt { get; set; }
    public DateTime date_modified { get; set; }
    public DateTime date_modified_gmt { 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 bool prices_include_tax { get; set; }
    public int customer_id { get; set; }
    public string customer_ip_address { get; set; }
    public string customer_user_agent { get; set; }
    public string customer_note { 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 DateTime date_paid { get; set; }
    public DateTime date_paid_gmt { get; set; }
    public DateTime date_completed { get; set; }
    public DateTime date_completed_gmt { get; set; }
    public string cart_hash { 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 string currency_symbol { get; set; }
    public Links _links { get; set; }
}
...