DataTable to Json using jquery

前端 未结 9 1205
抹茶落季
抹茶落季 2020-12-16 05:48

I\'m trying to execute a web service which returns a DataTable with the following piece of code:

$.ajax({  
    type: \"POST\",  
    url: url,  
    data: d         


        
相关标签:
9条回答
  • 2020-12-16 06:02

    Json.NET has the ability to write DataSets/DataTables to JSON.

    http://james.newtonking.com/archive/2008/09/06/dataset-datatable-serialization-with-json-net.aspx

    0 讨论(0)
  • 2020-12-16 06:03

    I must admit I'm not hugely surprised - DataTable basically breaks most of the rules of structured data. Why not simply project from the data-table into a typed object? A related question came up earlier... or if you know the schema of the DataTable just do the conversion in C#...

    Manually building the JSON might work, but there are a lot of edge-cases to avoid; I'd rather let an existing framework handle it, to be honest.

    0 讨论(0)
  • 2020-12-16 06:04

    Easiest way is to use the LINQ to DataSet extensions. First need to create a generic list (SearchSerialResults is just a DTO in this case) from the DataTable using LINQ to DataSet.

    var resultItems = (from DataRow dr in _returnedData.AsEnumerable() select new SearchSerialResults {
      ContractLineItem = (int) dr["fldContractLineItemID"],
        SearchItem = (string) dr["Search Item"],
        Customer = (string) dr["Customer"],
        DeviceFound = (string) dr["Device Found"],
        Country = (string) dr["Country"],
        City = (string) dr["City"],
        ContractNumber = (string) dr["Contract Number"],
        QuoteNumber = (string) dr["Quote Number"],
        BeginDate = (string) dr["Begin Date"],
        EndDate = (string) dr["End Date"]
    }).ToList();
    

    _returnedData is the DataTable in this case. Step 2 is to do the conversion. In this case, I am returning a Json object for a jqGrid.

    var jsonObject = new {
      total = totalPages,
        pageSize,
        records = totalRecords,
        rows = (from SearchSerialResults item in resultItems select new {
          id = item.ContractLineItem,
            cell = new [] {
              item.ContractLineItem.ToString(),
                item.SearchItem,
                item.DeviceFound,
                item.Customer,
                item.ContractNumber,
                item.QuoteNumber,
                item.Country,
                item.City,
                item.BeginDate,
                item.EndDate,
                ""
            }
        }).ToArray()
    };
    return Json(jsonObject) // for MVC
    
    0 讨论(0)
  • 2020-12-16 06:06

    .Net 3.5 has a JSONSerializer that should be able to handle a datatable. You may want to look at your service code again and try getting it to use that. Also, I put some code together to do it manually in this question.

    0 讨论(0)
  • 2020-12-16 06:09

    http://schotime.net/blog/index.php/2008/07/27/dataset-datatable-to-json/

    0 讨论(0)
  • 2020-12-16 06:16

    In the end, I've decided to use the JavaScriptSerializer class to convert the DataTable into a JSON string. Unfortunately, this class doesn't work with a DataTable so I converted the DataTable into a list of dictionnaries and pass that list to the JavaScriptSerializer class. It takes only a few lines of code and it works fine.
    Example in VB.net:

        Public Function GetJson(ByVal dt As DataTable) As String
    
            Dim serializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
            Dim rows As New List(Of Dictionary(Of String, Object))
            Dim row As Dictionary(Of String, Object)
    
            For Each dr As DataRow In dt.Rows
                row = New Dictionary(Of String, Object)
                For Each col As DataColumn In dt.Columns
                    row.Add(col.ColumnName, dr(col))
                Next
                rows.Add(row)
            Next
            Return serializer.Serialize(rows)
        End Function
    
    0 讨论(0)
提交回复
热议问题