DataTable for Json using jquery

I am trying to execute a web service that returns a DataTable with the following code snippet:

$.ajax({  
    type: "POST",  
    url: url,  
    data: data,   
    contentType: "application/json; charset=utf-8",  
    dataType: "json",  
    success: function(msg) {  
        //do things  
        }  
    }); 

If webservice returns a class, then it works, so it has nothing to do with input parameters, etc. It fails when the web method returns data (a datatable has only 2 columns and 2 rows for the test I do).

The WebService class is decorated with the [ScriptService] attribute, so I thought ASP.NET would automatically serialize the return value as JSON. It does not seem to work with data.

The only solution I found was to return a string (a manual serialized JSON object), but it seems to me that this is not the case. I am using Visual Studio 2008 with .Net 3.5

+5
9

, JavaScriptSerializer DataTable JSON. , DataTable, DataTable dictionnaries JavaScriptSerializer. , .
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
+8

- LINQ to DataSet. (SearchSerialResults - DTO ) DataTable, 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 - DataTable . 2 - . Json 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
+5

WebService

    Imports System.Web.Script.Serialization

    Dim wsServicio As New ["YourWsInstance"]
    Dim dsInstEstado As New DataSet
    Dim sSql As String

    sSql = " Your SQL Statement"
    dsInstEstado = wsServicio.getData("YourWebServiceParameters")
    Dim jsonString = DataTableToJSON(dsInstEstado.Tables("CA_INSTITUCION"))
    Return Json(jsonString, JsonRequestBehavior.AllowGet)

    Function DataTableToJSon(dt As DataTable) As Object
    Dim arr(dt.Rows.Count - 1) As Object
    Dim column As DataColumn
    For i = 0 To dt.Rows.Count - 1
        Dim dict As New Dictionary(Of String, Object)
        For Each column In dt.Columns
            dict.Add(column.ColumnName, dt.Rows(i)(column))
        Next
        arr(i) = dict
    Next
   Return arr
 End Function
+2

, - DataTable . ? A ... DataTable, #...

JSON , , ; , .

+1

.Net 3.5 JSONSerializer, . . , , .

0

, , DataTable webservice/json. Json.NET.

, json. , . , "" . , , , , json.

0

# :

[Serializable]
public class TableMethod
{
    private int m_total; public int total { get { return this.m_total; } set { this.m_total = value; } }
    private int m_page; public int page { get { return this.m_page; } set { this.m_page = value; } }
    private int m_records; public int records { get { return this.m_records; } set { this.m_records = value; } }
    private IList<RowElement> m_rows; public IList<RowElement> rows { get { return this.m_rows; } set { this.m_rows = value; } }
    public TableMethod()
    {
        this.m_records = 20;
        this.m_total = 20;
        this.m_page = 1;
    }
}
[Serializable]
public class RowElement
{
    public string id;
    public string[] cell;
}
0

All Articles