VB.NET中如何将Datatable转换为Json格式数据

需要引入

Imports System.Web.Script.Serialization
Imports System.Collections.Generic  

    Public Function Dtb2Json(ByVal dtb As DataTable) As String
        Dim jss As JavaScriptSerializer = New JavaScriptSerializer
        Dim dic As ArrayList = New ArrayList()
        For Each item As DataRow In dtb.Rows
            Dim drow As Dictionary(Of String, Object) = New Dictionary(Of String, Object)()
            For Each col As DataColumn In dtb.Columns
                drow.Add(col.ColumnName, item.Item(col.ColumnName))
            Next
            dic.Add(drow)
        Next
        Return jss.Serialize(dic)
    End Function

posted on 2013-09-10 16:56  麦克斯韦科思  阅读(2147)  评论(0编辑  收藏  举报