小编inn*_*ron的帖子

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
Run Code Online (Sandbox Code Playgroud)

但是这段代码给我带来了糟糕的json: [{"NAME":"city","PARENT":"address","VALUE":"has child"},{"NAME":"coordinates","PARENT":"address","VALUE":"has child"},{"NAME":"street","PARENT":"address","VALUE":"has child"}.......

有人可以帮助我吗?

vb.net datatable serialization json

7
推荐指数
2
解决办法
2万
查看次数

标签 统计

datatable ×1

json ×1

serialization ×1

vb.net ×1