vb.net datatable序列化为json

前端之家收集整理的这篇文章主要介绍了vb.net datatable序列化为json前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这种表:

我需要获得这个JSON(当然命令可以是任何,结构/树是最重要的):

数据表可以更改,因此序列化应该是动态的.我正在使用vb.net并使用此代码

Public Function GetJson() As String
        Dim dt As New System.Data.DataTable
        dt = CreateDataTable() 'here I retrive data from oracle DB
        Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
        Dim packet As New List(Of Dictionary(Of String,Object))()
        Dim row As Dictionary(Of String,Object) = Nothing
        For Each dr As DataRow In dt.Rows
            row = New Dictionary(Of String,Object)()
            For Each dc As DataColumn In dt.Columns
                row.Add(dc.ColumnName.Trim(),dr(dc))
            Next
            packet.Add(row)
        Next
        Return serializer.Serialize(packet)
    End Function

但是这段代码给我带来了不好的json:[{“NAME”:“city”,“PARENT”:“address”,“VALUE”:“have child”},{“NAME”:“coordinates”,“PARENT”:“地址“,”VALUE“:”有孩子“},{”NAME“:”街道“,”父母“:”地址“,”VALUE“:”有孩子“} …….

有人可以帮助我吗?

‘哦 – 不,你没有’版本:
Public Function GetJson(ByVal dt As DataTable) As String
    Return New JavaScriptSerializer().Serialize(From dr As DataRow In dt.Rows Select dt.Columns.Cast(Of DataColumn)().ToDictionary(Function(col) col.ColumnName,Function(col) dr(col)))
End Function

猜你在找的VB相关文章