vb.net 数据表序列化为 json [英] vb.net datatable Serialize to json

查看:23
本文介绍了vb.net 数据表序列化为 json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的桌子:

我需要得到这个 JSON(当然顺序可以是任意的,结构/树是最重要的):

I need to get this JSON (of course order could be any, structure/tree is most important):

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

Data table can change, so serialization should be dynamic. I am working with vb.net and used this code:

 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"}.......

But this code returns me bad json: [{"NAME":"city","PARENT":"address","VALUE":"has child"},{"NAME":"coordinates","PARENT":"address","VALUE":"has child"},{"NAME":"street","PARENT":"address","VALUE":"has child"}.......

有人可以帮我吗?

推荐答案

这是我的解决方案:

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

这篇关于vb.net 数据表序列化为 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆