-1

I am using table data from jQuery Datatable and passing it to an ajax function. The function calls a web service to process the data and I am stuck trying to deserialize it.

The initial call comes from datatable's 'buttons' button group, where I am trying to implement a custom export to Excel:

buttons: [
    {
        extend: "collection",
        text: "Export",
        buttons: [
            {
                text: 'My Excel',
                action: function (e, dt, node, config) {
                    exportToExcel(dt);
                }
            },
            ...
         ]

function exportToExcel(dt) {debugger
    // var tableRows = dt.rows().data().toArray();
    var tableRows = dt.data().toArray();
    var blah = JSON.stringify({ TableData: tableRows });

    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        cache: false,
        url: "../WebService/ABC.asmx/ExportToExcel",
        data: JSON.stringify({ TableData: blah }),
    }).done(function (result) {
    }).fail(...

I tried creating classes to see if that would work but no luck.

public string ExportToExcel(string TableData) 
{
    string JSONresult = string.Empty;
    //Errors_Counts[] oDict = (JsonConvert.DeserializeObject<Dictionary<string, Errors_Counts[]>>(TableData)).Values.FirstOrDefault();
    return JSONresult;
}

The data, once JSON-stringified looks like this:

{"TableData":
    [
        {"THEDATE":"2022-12-10T00:00:00","E38":0,"E39":1,"E40":37,"E41":0,"E42":0},
        {"THEDATE":"2022-12-11T00:00:00","E38":0,"E39":0,"E40":20,"E41":0,"E42":0},
        ...        
  ]
}

How would I deserialize this to end up with a dataset or data table that I can further process? Right now I just have a JSON string. The third party soaftware that I use for exporting to Excel works best with data table. So if I can end up with a data table with column names as TheDate, E38, E39, ... it would solve my issue.

dbc
  • 104,963
  • 20
  • 228
  • 340
NoBullMan
  • 2,032
  • 5
  • 40
  • 93
  • 1
    You want a `DataSet` but have you just tried deserializing to one? Because it seems like `var dataSet = JsonConvert.DeserializeObject(jsonString);` just works, see https://dotnetfiddle.net/q1SZ5c. See e.g. [this answer](https://stackoverflow.com/a/34287982/3744182) by [shA.t](https://stackoverflow.com/users/4519059/sha-t) to [How to parse a JSONString To Dataset?](https://stackoverflow.com/q/19136024/3744182). – dbc Dec 17 '22 at 07:42
  • Sorry, I meant [this answer](https://stackoverflow.com/a/19139494/3744182) by [Oualid KTATA](https://stackoverflow.com/users/1486031/oualid-ktata). – dbc Dec 17 '22 at 16:19
  • @dbc change your comment to answer and I will mark it. Thanks for the help. – NoBullMan Dec 17 '22 at 22:13

1 Answers1

-1

if you just need a DataTable instance

public string ExportToExcel(JObject TableData) 
{
//DataTable dataTable = TableData["TableData"].ToObject<DataTable>();

 return TableData["TableData"].ToString();
}
Serge
  • 40,935
  • 4
  • 18
  • 45