最后,我决定使用JavascriptSerializer类将DataTable转换为JSON字符串。不幸的是,该类不适用于DataTable,因此我将DataTable转换为字典列表,并将该列表传递给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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)