2016-07-22 7 views
0
{"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":"[email protected]","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":"[email protected]","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":"[email protected]","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":"[email protected]","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":"[email protected]","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":"[email protected]","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":"[email protected]","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":"[email protected]","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":"[email protected]","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":"[email protected]","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":"[email protected]","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":"[email protected]","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":"[email protected]","phone":""},"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":"[email protected]","first_name":"","last_name":"","username":"[email protected]","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":"[email protected]","phone":""},"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":"[email protected]","phone":""},"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":"[email protected]","first_name":"","last_name":"","username":"[email protected]","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":"[email protected]","phone":""},"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":"[email protected]","phone":""},"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":"[email protected]","first_name":"","last_name":"","username":"[email protected]","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":"[email protected]","phone":""},"shipping_address":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""}}}]} 

Ich bin in der Lage, diese Zeichenfolge Datentabelle zu konvertieren, aber ich bin nicht in der Lage, alle Daten zu erhalten, die innerhalb der Arrays in dem JSON-String ist, die Daten innerhalb des Arrays payment_details, billing_address, shipping_address, line_Items, shipping_lines, tax_lines, fee_lines, coupon_lines, Kunden und Arrays innerhalb des Kunden. kann mir jemand zeigen, wie man das macht. Ich brauche diese Daten für C# Windows-Anwendung. ich bin neu in diesem json Sachen so, wenn es eine Änderung in Frage fühlen sich frei zu bearbeitenWoocommerce json zu Datensatz oder Datentabelle

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()); 
    } 

dies ist der Code, die ich online gefunden und ich verwendet, um dieses die json zu Datatable

+0

Bitte teilen ** die C# -Code ** Sie bereits versucht haben, die nicht funktionierten. Von https://stackoverflow.com/help/how-to-ask: * Helfen Sie anderen, das Problem zu reproduzieren ... Fügen Sie nur so viel Code ein, dass andere das Problem reproduzieren können. * Außerdem können Sie JSON vereinfachen, um ein [ mcve] deines Problems? – dbc

+0

Ich habe den Beitrag editieren und den C# -Code eingefügt, bitte schauen Sie es sich an. Ich bin nicht in der Lage, einfach den JSON-Code Ich suchte Hilfe in diesem auch @ dbc –

+0

Das Problem ist, dass Ihre JSON verschachtelte * Objekte * anstatt verschachtelte * Arrays von Objekten * hat, zB: '[{" id ": 41, "nested_object": {"name": "HARSH"}}] 'anstelle von' [{"id": 41, "nested_array": [{"name": "HARSH"}]}] '. Ein verschachteltes Array kann in eine verschachtelte 'DataTable' konvertiert werden, aber es gibt keine kanonische Möglichkeit, ein verschachteltes Objekt darzustellen. Was willst du dort machen? – dbc

Antwort

0

Json.NET hat eine eingebaute DataTableConverter. So Sie

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

ein JArray zu einem DataTable indem jedoch umwandeln können, Ihre JSON hat zwei Probleme, die diese Umwandlung machen scheitern:

  1. Einige Ihrer Objekte haben leere Array-Eigenschaften, zum Beispiel:

    "meta": [] 
    

    Aus irgendeinem Grund (möglicherweise ein Bug?) diese verwirren den Konverter verursacht eine Ausnahme zu werfen. Da sie jedoch leer sind, können sie entfernt und außer Acht gelassen werden:

    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(); 
        } 
    } 
    
  2. Es hat sich Objekte in Objekte verschachtelt. Vereinfachen stark, haben Sie die folgenden Schritte aus:

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

    Leider gibt es keine kanonische Weise ist diese Objekthierarchie auf eine DataTable zu konvertieren. Es ist möglich, Datentabellen ineinander zu verschachteln, jedoch wird dies kanonisch einem verschachtelten JSON Array zugeordnet, anstatt einem verschachtelten Objekt. In Kommentare, die Sie geschrieben haben Ich muss diese Daten in SQL Server einfügen. Also, wie wollen Sie, dass diese Daten in der Datenbank modelliert werden?

    Eine Möglichkeit wäre, die verschachtelten Objekte in verschachtelte Arrays von Objekten zu konvertieren. Danach ist eine automatische Konvertierung möglich. Es wird jedoch nun angezeigt, dass jedes Ihrer order Objekte mehrere (z. B.) Kunden haben kann, wenn dies tatsächlich nicht der Fall ist.Wenn dies zufriedenstellend ist, dann kann das folgende Verfahren verwendet werden:

    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); 
        } 
    } 
    

    Es wird folgende JSON erzeugt:

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

    Alternativ können Sie verschachtelte Objekte glätten durch ihre Eigenschaften sprudeln zu ihrem Eltern. Die folgende Methode funktioniert dies:

    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); 
         } 
        } 
    } 
    

    Es produziert die folgende JSON, die erfolgreich auf eine DataTable umgewandelt werden kann:

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

    Beachten Sie, dass die „id“ Eigenschaft des „Kunden“ Objekt wurde "customer.id".

    Als dritte Option könnten Sie versuchen, geschachtelte Objekte als Dictionary<string, object> deserialisieren - obwohl ich keine Ahnung habe, ob der SQL Server-Adapter eine solche Datentabelle verarbeiten könnte. Um dies zu tun, müssen Sie die Logik von DataTableConverter.ReadJson() nach den Vorgaben der Antwort manuell auf DateTime column type becomes String type after deserializing DataTable replizieren und GetColumnDataType(JsonReader reader) ändern, um typeof(Dictionary<string, object>) für JsonToken.StartObject zurückzugeben.

So kann Ihr Tabulate() Methode wird:

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>(); 
} 

Oder vielleicht:

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>(); 
} 
+0

Dank @dbc für Ihre Hilfe diese Methode von Ihnen funktioniert und ich bin in der Lage, alle Daten zu erhalten in der Datentabelle Vielen Dank für Ihre Hilfe und Unterstützung –

+0

Ich bemerkte, dass die Daten in der LineItems-Array in der Datentabelle fehlt, die ich mit Hilfe der Funktion FlattenTablute() von Ihrem können Sie mir wieder bitte helfen. alle anderen Daten sind nur verfügbar, die im Abschnitt Lintitems nicht @ dbc erhalten –

+0

@Harshkantharia - die '" line_items "' sind ein verschachteltes Array von Objekten, kein verschachteltes Objekt. Sie werden erfolgreich von Json.NET als verschachtelte 'DataTable' deserialisiert. Wenn Sie 'Debug.WriteLine (JsonConvert.SerializeObject (dt, Formatting.Indured))' auf der zurückgegebenen 'DataTable' tun, werden Sie sie sehen. Wenn sie nicht in Ihre Datenbank eingefügt werden, muss das daran liegen, wie Sie das Einfügen durchführen. Da das bevorzugte Format hier [eine Frage pro Beitrag] ist (https://meta.stackexchange.com/questions/222735), schlage ich vor, dass Sie eine zweite Frage stellen, warum die Einfügung fehlschlägt. – dbc

0

Als Ihr zu konvertieren Die Saite ist zu lang Ich benutze eine kürzere Saite (nehme von deiner), die einfacher zu verfolgen ist und dir den Hinweis gibt.

Zuerst installieren Sie die Json.NET (dies ist nicht die einzige Option, aber es ist eine der besten) mit der Nuget-Paket-Management. Dann können Sie die Zeichenfolge leicht analysieren. Ich denke, die folgenden Schnipsel geben dir eine Idee.

using System; 
using Newtonsoft.Json.Converters; 

namespace Test 
{ 
    class JsonParser 
    { 
     public static void Main(string[] args) 
     { 
      string jsonStr = @"{'orders':[ 
          {'id':41, 
          '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':'[email protected]','phone':'9723638788'}, 
          }]}"; 
      dynamic data = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonStr); 
      Console.WriteLine(data.orders[0].billing_address.first_name); 
      Console.Read(); 
     }   
} 
+0

Nun, das holt mich Daten, aber ich brauche diese Daten in einer Datentabelle oder Dataset, wie ich diese Daten in SQL Server einfügen müssen –

Verwandte Themen