vb.net datatable序列化为json [英] vb.net datatable Serialize to json
问题描述
我有这样的表:
我需要得到这个JSON(当然订单可以是任何,结构/树是最重要的):
数据表可以更改,所以序列化应该是动态的。我正在使用vb.net并使用以下代码:
公共函数GetJson()As String
Dim dt As新的System.Data.DataTable
dt = CreateDataTable()'这里我从oracle DB中读取数据
Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim packet As New List(Of String,Object))()
Dim row As Dictionary(Of String,Object)= Nothing
对于每个dr As DataRow在dt.Rows
row =新字典(Of String,Object)()
对于每个dc作为DataColumn在dt.Columns
row.Add(dc.ColumnName.Trim(),dr(dc))
下一个
packet.Add(row)
下一个
返回serializer.Serialize(packet)
结束函数
但是这个代码返回我不好json: [{NAME:city,PARENT:address,VALUE:have child}, {NAME:coordinates,PARENT:address,VAL UE:have child},{NAME:street,PARENT:address,VALUE:has child} .......
/ p>
有人可以帮助我吗?
这是我的解决方案:
公共函数GetJson()As String
Dim dt As New System.Data.DataTable
dt = CreateDataTable()'在这里我从oracle DB中获取数据
Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim packet As New List(Of Dictionary String,Object))()
Dim row As Dictionary(Of String,Object)= Nothing
尝试
Dim result()As DataRow = dt.Select(Parent_ = 'main')
Dim i As Integer
For i = 0 To result.GetUpperBound(0)
row =新字典(Of String,Object)()
如果UCase (结果(i)(2))< HAS CHILD然后
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
返回serializer.Serialize(packet)
Catch ex As Exception
MsgBox(ex.ToString)
Me.Close()
结束尝试
结束函数
公共函数add_(ByVal dt As System.Data.DataTable,ByVal parent_ As String)As Dictionary(Of String,Object)
Dim row As Dictionary(Of String,Object)= Nothing
尝试
Dim result()As DataRow = dt.Select(Parent_ ='& parent_&')
Dim i As Integer
row =新字典(Of String,Object)( )
For i = 0 To result.GetUpperBound(0)
如果UCase(result(i)(2))< HAS CHILD然后
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
返回行
结束函数
I have this kind of table:
I need to get this JSON (of course order could be any, structure/tree is most important):
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
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"}.......
Can someone help me out in here?
Here is my solution:
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 datatable序列化为json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!