我有这样一张表:
我需要获取此JSON(当然,顺序可以是任何的,结构/树最为重要):
数据表可能会变化,因此序列化应该是动态的。 我正在使用vb.net并使用了以下代码:
![](https://istack.dev59.com/B1ShR.webp)
![](https://istack.dev59.com/Q16ui.webp)
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"}.......
有人能帮我解决这个问题吗?