Woo Commerce JSON到数据集或数据表 [英] Woo commerce json to Dataset or datatable

查看:44
本文介绍了Woo Commerce JSON到数据集或数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{"orders":[{"id":41,"order_number":41,"created_at":"2016-07-21T06:34:43Z","updated_at":"2016-07-21T06:34:43Z","completed_at":"2016-07-21T06:34:43Z","status":"processing","currency":"INR","total":"25000.00","subtotal":"25000.00","total_line_items_quantity":10,"total_tax":"0.00","total_shipping":"0.00","cart_tax":"0.00","shipping_tax":"0.00","total_discount":"0.00","shipping_methods":"","payment_details":{"method_id":"cod","method_title":"Cash on Delivery","paid":false},"billing_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN","email":"harsh@resolutesolutions.in","phone":"9723638788"},"shipping_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN"},"note":"","customer_ip":"43.239.77.131","customer_user_agent":"Mozilla\/5.0 (Windows NT 6.1; rv:48.0) Gecko\/20100101 Firefox\/48.0","customer_id":2,"view_order_url":"http:\/\/woocommerce-5468-12693-128023.cloudwaysapps.com\/my-account\/view-order\/41","line_items":[{"id":8,"subtotal":"25000.00","subtotal_tax":"0.00","total":"25000.00","total_tax":"0.00","price":"2500.00","quantity":10,"tax_class":null,"name":"Yellow Saree","product_id":27,"sku":"","meta":[]}],"shipping_lines":[],"tax_lines":[],"fee_lines":[],"coupon_lines":[],"customer":{"id":2,"created_at":"2016-07-21T05:58:17Z","email":"harsh@resolutesolutions.in","first_name":"HARSH","last_name":"KANTHARIA","username":"harsh","role":"customer","last_order_id":"41","last_order_date":"2016-07-21T06:34:43Z","orders_count":3,"total_spent":"29600.00","avatar_url":"http:\/\/2.gravatar.com\/avatar\/?s=96","billing_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN","email":"harsh@resolutesolutions.in","phone":"9723638788"},"shipping_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ACE GROUP","address_1":"20 DHANLAXMI BUNGLOWS","address_2":"PAL","city":"SURAT","state":"GJ","postcode":"395009","country":"IN"}}},{"id":40,"order_number":40,"created_at":"2016-07-21T06:01:28Z","updated_at":"2016-07-21T06:01:28Z","completed_at":"2016-07-21T06:01:28Z","status":"processing","currency":"INR","total":"3600.00","subtotal":"3600.00","total_line_items_quantity":2,"total_tax":"0.00","total_shipping":"0.00","cart_tax":"0.00","shipping_tax":"0.00","total_discount":"0.00","shipping_methods":"","payment_details":{"method_id":"cod","method_title":"Cash on Delivery","paid":false},"billing_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN","email":"harsh@resolutesolutions.in","phone":"9723638788"},"shipping_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN"},"note":"","customer_ip":"43.239.77.131","customer_user_agent":"Mozilla\/5.0 (Windows NT 6.1; rv:48.0) Gecko\/20100101 Firefox\/48.0","customer_id":2,"view_order_url":"http:\/\/woocommerce-5468-12693-128023.cloudwaysapps.com\/my-account\/view-order\/40","line_items":[{"id":6,"subtotal":"2100.00","subtotal_tax":"0.00","total":"2100.00","total_tax":"0.00","price":"2100.00","quantity":1,"tax_class":null,"name":"Orange Saree","product_id":31,"sku":"","meta":[]},{"id":7,"subtotal":"1500.00","subtotal_tax":"0.00","total":"1500.00","total_tax":"0.00","price":"1500.00","quantity":1,"tax_class":null,"name":"Test product","product_id":10,"sku":"testproduct","meta":[]}],"shipping_lines":[],"tax_lines":[],"fee_lines":[],"coupon_lines":[],"customer":{"id":2,"created_at":"2016-07-21T05:58:17Z","email":"harsh@resolutesolutions.in","first_name":"HARSH","last_name":"KANTHARIA","username":"harsh","role":"customer","last_order_id":"41","last_order_date":"2016-07-21T06:34:43Z","orders_count":3,"total_spent":"29600.00","avatar_url":"http:\/\/1.gravatar.com\/avatar\/?s=96","billing_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN","email":"harsh@resolutesolutions.in","phone":"9723638788"},"shipping_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ACE GROUP","address_1":"20 DHANLAXMI BUNGLOWS","address_2":"PAL","city":"SURAT","state":"GJ","postcode":"395009","country":"IN"}}},{"id":39,"order_number":39,"created_at":"2016-07-21T05:58:17Z","updated_at":"2016-07-21T05:58:17Z","completed_at":"2016-07-21T05:58:17Z","status":"processing","currency":"INR","total":"1000.00","subtotal":"1000.00","total_line_items_quantity":1,"total_tax":"0.00","total_shipping":"0.00","cart_tax":"0.00","shipping_tax":"0.00","total_discount":"0.00","shipping_methods":"","payment_details":{"method_id":"cod","method_title":"Cash on Delivery","paid":false},"billing_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN","email":"harsh@resolutesolutions.in","phone":"9723638788"},"shipping_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN"},"note":"","customer_ip":"43.239.77.131","customer_user_agent":"Mozilla\/5.0 (Windows NT 6.1; rv:48.0) Gecko\/20100101 Firefox\/48.0","customer_id":2,"view_order_url":"http:\/\/woocommerce-5468-12693-128023.cloudwaysapps.com\/my-account\/view-order\/39","line_items":[{"id":5,"subtotal":"1000.00","subtotal_tax":"0.00","total":"1000.00","total_tax":"0.00","price":"1000.00","quantity":1,"tax_class":null,"name":"Test Saree product","product_id":15,"sku":"","meta":[]}],"shipping_lines":[],"tax_lines":[],"fee_lines":[],"coupon_lines":[],"customer":{"id":2,"created_at":"2016-07-21T05:58:17Z","email":"harsh@resolutesolutions.in","first_name":"HARSH","last_name":"KANTHARIA","username":"harsh","role":"customer","last_order_id":"41","last_order_date":"2016-07-21T06:34:43Z","orders_count":3,"total_spent":"29600.00","avatar_url":"http:\/\/1.gravatar.com\/avatar\/?s=96","billing_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN","email":"harsh@resolutesolutions.in","phone":"9723638788"},"shipping_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ACE GROUP","address_1":"20 DHANLAXMI BUNGLOWS","address_2":"PAL","city":"SURAT","state":"GJ","postcode":"395009","country":"IN"}}},{"id":38,"order_number":38,"created_at":"2016-07-21T05:24:29Z","updated_at":"2016-07-21T05:24:29Z","completed_at":"2016-07-21T05:24:29Z","status":"processing","currency":"INR","total":"3600.00","subtotal":"3600.00","total_line_items_quantity":2,"total_tax":"0.00","total_shipping":"0.00","cart_tax":"0.00","shipping_tax":"0.00","total_discount":"0.00","shipping_methods":"","payment_details":{"method_id":"cod","method_title":"Cash on Delivery","paid":false},"billing_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN","email":"harsh@resolutesolutions.in","phone":"9723638788"},"shipping_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN"},"note":"","customer_ip":"43.239.77.131","customer_user_agent":"Mozilla\/5.0 (Windows NT 6.1; rv:48.0) Gecko\/20100101 Firefox\/48.0","customer_id":0,"view_order_url":"http:\/\/woocommerce-5468-12693-128023.cloudwaysapps.com\/my-account\/view-order\/38","line_items":[{"id":4,"subtotal":"3600.00","subtotal_tax":"0.00","total":"3600.00","total_tax":"0.00","price":"1800.00","quantity":2,"tax_class":null,"name":"Black Saree","product_id":23,"sku":"DM3103","meta":[]}],"shipping_lines":[],"tax_lines":[],"fee_lines":[],"coupon_lines":[],"customer":{"id":0,"email":"harsh@resolutesolutions.in","first_name":"HARSH","last_name":"KANTHARIA","billing_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN","email":"harsh@resolutesolutions.in","phone":"9723638788"},"shipping_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN"}}},{"id":37,"order_number":37,"created_at":"2016-07-20T08:17:21Z","updated_at":"2016-07-20T08:17:21Z","completed_at":"2016-07-20T08:17:21Z","status":"processing","currency":"INR","total":"2100.00","subtotal":"2100.00","total_line_items_quantity":1,"total_tax":"0.00","total_shipping":"0.00","cart_tax":"0.00","shipping_tax":"0.00","total_discount":"0.00","shipping_methods":"","payment_details":{"method_id":"cod","method_title":"Cash on Delivery","paid":false},"billing_address":{"first_name":"Rahul","last_name":"Patel","company":"Resolute Solutions","address_1":"surat","address_2":"","city":"Surat","state":"GJ","postcode":"395001","country":"IN","email":"info@resolutesolutions.in","phone":"9874563210"},"shipping_address":{"first_name":"Rahul","last_name":"Patel","company":"Resolute Solutions","address_1":"surat","address_2":"","city":"Surat","state":"GJ","postcode":"395001","country":"IN"},"note":"","customer_ip":"43.239.77.131","customer_user_agent":"Mozilla\/5.0 (Windows NT 6.3; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/51.0.2704.103 Safari\/537.36","customer_id":1,"view_order_url":"http:\/\/woocommerce-5468-12693-128023.cloudwaysapps.com\/my-account\/view-order\/37","line_items":[{"id":3,"subtotal":"2100.00","subtotal_tax":"0.00","total":"2100.00","total_tax":"0.00","price":"2100.00","quantity":1,"tax_class":null,"name":"Orange Saree","product_id":31,"sku":"","meta":[]}],"shipping_lines":[],"tax_lines":[],"fee_lines":[],"coupon_lines":[],"customer":{"id":1,"created_at":"2016-06-27T05:01:55Z","email":"info@resolutesolutions.in","first_name":"","last_name":"","username":"info@resolutesolutions.in","role":"administrator","last_order_id":"37","last_order_date":"2016-07-20T08:17:21Z","orders_count":3,"total_spent":"7300.00","avatar_url":"http:\/\/0.gravatar.com\/avatar\/?s=96","billing_address":{"first_name":"Rahul","last_name":"Patel","company":"Resolute Solutions","address_1":"surat","address_2":"","city":"Surat","state":"GJ","postcode":"395001","country":"IN","email":"info@resolutesolutions.in","phone":"9874563210"},"shipping_address":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""}}},{"id":35,"order_number":35,"created_at":"2016-06-27T08:09:00Z","updated_at":"2016-06-27T13:46:28Z","completed_at":"2016-06-27T13:46:28Z","status":"completed","currency":"INR","total":"4200.00","subtotal":"4200.00","total_line_items_quantity":2,"total_tax":"0.00","total_shipping":"0.00","cart_tax":"0.00","shipping_tax":"0.00","total_discount":"0.00","shipping_methods":"","payment_details":{"method_id":"cod","method_title":"Cash on Delivery","paid":false},"billing_address":{"first_name":"Rahul","last_name":"Patel","company":"Resolute Solutions","address_1":"surat","address_2":"","city":"Surat","state":"GJ","postcode":"395001","country":"IN","email":"info@resolutesolutions.in","phone":"9874563210"},"shipping_address":{"first_name":"Rahul","last_name":"Patel","company":"Resolute Solutions","address_1":"surat","address_2":"","city":"Surat","state":"GJ","postcode":"395001","country":"IN"},"note":"","customer_ip":"43.239.77.131","customer_user_agent":"Mozilla\/5.0 (Windows NT 6.3; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/51.0.2704.103 Safari\/537.36","customer_id":1,"view_order_url":"http:\/\/woocommerce-5468-12693-128023.cloudwaysapps.com\/my-account\/view-order\/35","line_items":[{"id":2,"subtotal":"4200.00","subtotal_tax":"0.00","total":"4200.00","total_tax":"0.00","price":"2100.00","quantity":2,"tax_class":null,"name":"Orange Saree","product_id":31,"sku":"","meta":[]}],"shipping_lines":[],"tax_lines":[],"fee_lines":[],"coupon_lines":[],"customer":{"id":1,"created_at":"2016-06-27T05:01:55Z","email":"info@resolutesolutions.in","first_name":"","last_name":"","username":"info@resolutesolutions.in","role":"administrator","last_order_id":"37","last_order_date":"2016-07-20T08:17:21Z","orders_count":3,"total_spent":"7300.00","avatar_url":"http:\/\/1.gravatar.com\/avatar\/?s=96","billing_address":{"first_name":"Rahul","last_name":"Patel","company":"Resolute Solutions","address_1":"surat","address_2":"","city":"Surat","state":"GJ","postcode":"395001","country":"IN","email":"info@resolutesolutions.in","phone":"9874563210"},"shipping_address":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""}}},{"id":19,"order_number":19,"created_at":"2016-06-27T05:27:34Z","updated_at":"2016-06-27T05:27:34Z","completed_at":"2016-06-26T23:57:34Z","status":"processing","currency":"INR","total":"1000.00","subtotal":"1000.00","total_line_items_quantity":1,"total_tax":"0.00","total_shipping":"0.00","cart_tax":"0.00","shipping_tax":"0.00","total_discount":"0.00","shipping_methods":"","payment_details":{"method_id":"cod","method_title":"Cash on Delivery","paid":false},"billing_address":{"first_name":"Rahul","last_name":"Patel","company":"Resolute Solutions","address_1":"surat","address_2":"","city":"Surat","state":"GJ","postcode":"395001","country":"IN","email":"info@resolutesolutions.in","phone":"9874563210"},"shipping_address":{"first_name":"Rahul","last_name":"Patel","company":"Resolute Solutions","address_1":"surat","address_2":"","city":"Surat","state":"GJ","postcode":"395001","country":"IN"},"note":"","customer_ip":"43.239.77.131","customer_user_agent":"Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko\/20100101 Firefox\/44.0","customer_id":1,"view_order_url":"http:\/\/woocommerce-5468-12693-128023.cloudwaysapps.com\/my-account\/view-order\/19","line_items":[{"id":1,"subtotal":"1000.00","subtotal_tax":"0.00","total":"1000.00","total_tax":"0.00","price":"1000.00","quantity":1,"tax_class":null,"name":"Test Saree product","product_id":15,"sku":"","meta":[]}],"shipping_lines":[],"tax_lines":[],"fee_lines":[],"coupon_lines":[],"customer":{"id":1,"created_at":"2016-06-27T05:01:55Z","email":"info@resolutesolutions.in","first_name":"","last_name":"","username":"info@resolutesolutions.in","role":"administrator","last_order_id":"37","last_order_date":"2016-07-20T08:17:21Z","orders_count":3,"total_spent":"7300.00","avatar_url":"http:\/\/1.gravatar.com\/avatar\/?s=96","billing_address":{"first_name":"Rahul","last_name":"Patel","company":"Resolute Solutions","address_1":"surat","address_2":"","city":"Surat","state":"GJ","postcode":"395001","country":"IN","email":"info@resolutesolutions.in","phone":"9874563210"},"shipping_address":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""}}}]}

我能够将这些字符串转换为数据表,但是我无法获取json字符串数组中的所有数据,数组中的数据payment_details,billing_address,shipping_address,line_Items,shipping_lines,tax_lines,fee_lines,coupon_lines,customer和客户内部的数组.谁能告诉我方法如何做到这一点.我需要用于C#Windows应用程序的数据.我是json的新手,所以如果有任何问题,随时可以编辑

I am able to convert these string to datatable but i am not able to get all data which is inside the arrays in the json string, the data inside the arrays payment_details,billing_address,shipping_address,line_Items,shipping_lines,tax_lines,fee_lines,coupon_lines,customer and arrays inside customer. can anyone show me method how to do that. i need that data for c# windows application. i am new to this json stuff so if there is any change in question feel free to edit

  public static DataTable Tabulate(string json)
    {
        var jsonLinq = JObject.Parse(json);

        // Find the first array using Linq
        var srcArray = jsonLinq.Descendants().Where(d => d is JArray).First();
        var trgArray = new JArray();
        foreach (JObject row in srcArray.Children<JObject>())
        {
            var cleanRow = new JObject();
            foreach (JProperty column in row.Properties())
            {
                // Only include JValue types
                if (column.Value is JValue)
                {
                    cleanRow.Add(column.Name, column.Value);
                }
            }

            trgArray.Add(cleanRow);
        }

        return JsonConvert.DeserializeObject<DataTable>(trgArray.ToString());
    }

这是我在网上找到的代码,我用它来将json转换为数据表

this is the code that i found online and i used this to convert the json to datatable

推荐答案

Json.NET具有内置的 DataTableConverter .因此,您可以通过

Json.NET has a built-in DataTableConverter. Thus you can convert a JArray to a DataTable by doing

var table = trgArray.ToObject<DataTable>();

但是,您的JSON有两个问题使转换失败:

However, your JSON has two problems that make this conversion fail:

  1. 您的某些对象具有空数组属性,例如:

  1. Some of your objects have empty array properties, for instance:

"meta": []

由于某种原因(可能是错误?),这些会使转换器困惑,导致其引发异常.但是,由于它们为空,因此可以将其删除并忽略:

For some reason (possibly a bug?) these confuse the converter causing it to throw an exception. However, since they are empty, they can be removed and ignored:

public static void RemoveEmptyArrayProperties(JContainer root)
{
    var query = root.DescendantsAndSelf()
        .OfType<JProperty>()
        .Where(p => p.Value is JArray && ((JArray)p.Value).Count == 0);
    foreach (var property in query.ToList())
    {
        property.Remove();
    }
}

  • 它具有嵌套在对象内部的对象.大大简化了,您具有以下条件:

  • It has objects nested inside objects. Simplifying greatly, you have the following:

    [
      {
        "id": 41,
        "customer": {
          "id": 2
        }
      }
    ]
    

    不幸的是,没有规范的方法可以将该对象层次结构转换为 DataTable .可以将数据表彼此嵌套,但这可以规范地映射到嵌套的JSON array 而不是嵌套的 object .在您写的注释中,我需要在SQL Server中插入该数据.那么,您如何希望在数据库中对这些数据进行建模?

    Unfortunately, there is no canonical way to convert this object hierarchy to a DataTable. It is possible to nest data tables inside each other, however this canonically maps to a nested JSON array rather than a nested object. In comments you wrote I need to insert that data in SQL Server. So, how do you want this data to be modeled in the database?

    一种可能性是将嵌套的对象转换为嵌套的对象数组.完成此操作后,将可以进行自动转换.但是,现在看来,每个 order 对象实际上都可以拥有多个(例如)客户.如果满意,可以使用以下方法:

    One possibility would be to convert the nested objects to nested arrays of objects. Having done this, automatic conversion will be possible. However, it will now appear that each of your order objects can have multiple (e.g.) customers when this is in fact not true. If this is satisfactory then the following method can be used:

    public static void ConvertObjectPropertiesToArrays(JContainer root)
    {
        var query = root.DescendantsAndSelf()
            .OfType<JProperty>()
            .Where(p => p.Parent is JObject && p.Value is JObject);
        foreach (var property in query.ToList())
        {
            var value = property.Value;
            var array = new JArray();
            property.Value = array;
            array.Add(value);
        }
    }
    

    它产生以下JSON:

    [
      {
        "id": 41,
        "customer": [
          {
            "id": 2
          }
        ]
      }
    ]
    

    或者,您可以通过将嵌套对象的属性冒泡到其父对象来展平它们.使用以下方法可以做到这一点:

    Alternatively, you could flatten nested objects by bubbling their properties up to their parents. The following method does this:

    public static void FlattenObjectPropertiesToParents(JContainer root)
    {
        var query = root.DescendantsAndSelf()
            .OfType<JProperty>()
            .Where(p => p.Parent is JObject && p.Value is JObject)
            .ToList();
        for (int i = query.Count - 1; i >= 0; i--)
        {
            var property = query[i];
            var value = (JObject)property.Value;
            var parent = (JObject)property.Parent;
            property.Remove();
            foreach (var item in value.Properties().ToList())
            {
                item.Remove();
                parent.Add(string.Format("{0}.{1}", property.Name, item.Name), item.Value);
            }
        }
    }
    

    它会产生以下JSON,这些JSON可成功转换为 DataTable :

    It produces the following JSON which can be converted successfully to a DataTable:

    [
      {
        "id": 41,
        "customer.id": 2
      }
    ]
    

    请注意,客户"对象的"id"属性已变为"customer.id".

    Notice that the "id" property of the "customer" object has become "customer.id".

    作为第三种选择,您可以尝试将嵌套对象反序列化为 Dictionary< string,object> -尽管我不知道SQL Server适配器是否可以处理这样的数据表.为此,您需要按照DataTableConverter.ReadJson()的逻辑-column-type-becomes-string-type-after-deserializing-datatable/37126529#37126529>在对DataTable反序列化后,DateTime列类型变为String类型并修改

    As a third option, you could try deserializing nested objects as a Dictionary<string, object> -- though I have no idea whether the SQL Server adapter could handle such a data table. To do this, you will need to manually replicate the logic of DataTableConverter.ReadJson() along the lines of the answer to DateTime column type becomes String type after deserializing DataTable and modify GetColumnDataType(JsonReader reader) to return typeof(Dictionary<string, object>) for JsonToken.StartObject.

    因此,您的 Tabulate()方法变为:

    public static DataTable NestedTabulate(string json)
    {
        // Find the first array using Linq
        var jsonLinq = (JContainer)JToken.Parse(json);
        var srcArray = jsonLinq.DescendantsAndSelf().OfType<JArray>().First();
    
        RemoveEmptyArrayProperties(srcArray);
    
        ConvertObjectPropertiesToArrays(srcArray);
    
        return srcArray.ToObject<DataTable>();
    }
    

    或者可能:

    public static DataTable FlattenTabulate(string json)
    {
        // Find the first array using Linq
        var jsonLinq = (JContainer)JToken.Parse(json);
        var srcArray = jsonLinq.DescendantsAndSelf().OfType<JArray>().First();
    
        RemoveEmptyArrayProperties(srcArray);
    
        FlattenObjectPropertiesToParents(srcArray);
    
        return srcArray.ToObject<DataTable>();
    }
    

    这篇关于Woo Commerce JSON到数据集或数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆