vb.net 数据表序列化为 json

Posted

技术标签:

【中文标题】vb.net 数据表序列化为 json【英文标题】:vb.net datatable Serialize to json 【发布时间】:2014-03-06 01:32:14 【问题描述】:

我有这种桌子:

我需要得到这个 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":"has child","NAME":"coordinates","PARENT":"address","VALUE":"has child","NAME":"street","PARENT":"address","VALUE":"has child".......

有人可以帮帮我吗?

【问题讨论】:

【参考方案1】:

这是我的解决方案:

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

    Try
        Dim result() As DataRow = dt.Select("Parent_ = 'main'")
        Dim i As Integer
        For i = 0 To result.GetUpperBound(0)
            row = New Dictionary(Of String, Object)()
            If UCase(result(i)(2)) <> "HAS CHILD" Then
                row.Add(result(i)(0), result(i)(2))
            Else
                row.Add(result(i)(0), add_(dt, result(i)(0)))
            End If
            packet.Add(row)
        Next i

        Return serializer.Serialize(packet)
    Catch ex As Exception
        MsgBox(ex.ToString)
        Me.Close()
    End Try

End Function

Public Function add_(ByVal dt As System.Data.DataTable, ByVal parent_ As String) As Dictionary(Of String, Object)
    Dim row As Dictionary(Of String, Object) = Nothing
    Try
        Dim result() As DataRow = dt.Select("Parent_ = '" & parent_ & "'")
        Dim i As Integer
        row = New Dictionary(Of String, Object)()
        For i = 0 To result.GetUpperBound(0)

            If UCase(result(i)(2)) <> "HAS CHILD" Then
                row.Add(result(i)(0), result(i)(2))
            Else
                row.Add(result(i)(0), add_(dt, result(i)(0)))
            End If
        Next i
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
    Return row
End Function

【讨论】:

【参考方案2】:

“哦,不,你没有”版本:

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.net 数据表序列化为 json的主要内容,如果未能解决你的问题,请参考以下文章

VB.NET 从 ODBC 获取数据为 JSON

在 Visual Basic .NET 中反序列化 JSON

NewtonSoft.JSON 反序列化 - 未正确反序列化。 (VB.NET)

VB.NET 序列化和保存

将 xml 数据序列化为 json 时忽略命名空间属性

Swift 2:将 JSON 原生序列化为核心数据模型对象