1

我在 ASP.NET 中有这个函数可以将 a 转换DataTable为 JSON 字符串:

Public Function GetJson(ByVal dt As DataTable) As String
    Dim serializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
    Dim rows As New List(Of Dictionary(Of String, Object))
    Dim row As Dictionary(Of String, Object)
    Try
        For Each dr As DataRow In dt.Rows
            row = New Dictionary(Of String, Object)
            For Each col As DataColumn In dt.Columns
                row.Add(col.ColumnName, dr(col))
            Next
            rows.Add(row)
        Next
        Return serializer.Serialize(rows)
    Catch ex As Exception
        logFile("SP GetJson ----" + ex.Message)
        Return "-1"
    End Try
End Function

我的json会是这样的:

 {
"category": [
    {
        "id": "1",
        "desc": "default",
    },
    {
        "id": "2",
        "desc": "fun",
    }
],
"images": [
    {
        "image ID": "1",
        "link": "images/logo.jpg"
        "category": "1"
    },
    {
        "image ID": "2",
        "link": "images/logo2.jpg"
        "category": "2"
    }
]
}

但是现在我有一个 2DataTable的列表,我需要将其转换为一个具有 2 个数组的 JSON 字符串,有什么想法吗?

4

2 回答 2

3

您正在寻找的结构是现在创建的列表字典。尝试反序列化您的结构(更正了 json 字符串)

Dim serializer As New Web.Script.Serialization.JavaScriptSerializer()
Dim target As String = "{'category':[{'id':'1','desc':'default'},{'id':'2','desc':'fun'}],'images':[{'imageID':'1','link':'images/logo.jpg','category':'1'},{'imageID':'2','link':'images/logo2.jpg','category':'2'}]}"
Dim Desired = serializer.Deserialize(Of Object)(target)

Desired现在是我需要创建的结构来获取所需的 JSON 字符串。现在我创建表(它们在数据集中并且在 JSON 中存在名称)

Dim Ds As New DataSet
For Each kvp As KeyValuePair(Of String, Object) In Desired
    Dim tbl As New DataTable(kvp.Key)
    Ds.Tables.Add(tbl)
    For Each drow As Dictionary(Of String, Object) In kvp.Value
        If tbl.Rows.Count = 0 Then
            For Each Name As String In drow.Keys
                tbl.Columns.Add(Name, GetType(Object))
            Next
        End If
        Dim tblRow As DataRow = tbl.NewRow
        For Each fld As KeyValuePair(Of String, Object) In drow
            tblRow(fld.Key) = fld.Value
        Next
        tbl.Rows.Add(tblRow)
    Next
Next

到目前为止,我只是在准备数据。以下是想要的答案。

现在我创建结构并将其序列化。

Dim Result As New Dictionary(Of String, Object)
For Each tbl As DataTable In Ds.Tables
    Result.Add(tbl.TableName, GetRows(tbl))
Next

Dim serialized As String = serializer.Serialize(Result)

GetRows修改自GetJson

Public Function GetRows(ByVal dt As DataTable) As List(Of Dictionary(Of String, Object))
    Dim rows As New List(Of Dictionary(Of String, Object))
    Dim row As Dictionary(Of String, Object)
    For Each dr As DataRow In dt.Rows
        row = New Dictionary(Of String, Object)
        For Each col As DataColumn In dt.Columns
            row.Add(col.ColumnName, dr(col))
        Next
        rows.Add(row)
    Next
    Return rows
End Function
于 2013-10-24T07:12:56.933 回答
0
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim serializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
    Dim rows As New List(Of Dictionary(Of String, Object))
    GetJson(dt1, rows)
    GetJson(dt2, rows)
    Dim str As String = serializer.Serialize(rows)

End Sub

Public Sub GetJson(ByVal dt As DataTable, ByRef rows As List(Of Dictionary(Of String, Object)))

    Try
        Dim row As New Dictionary(Of String, Object)
        For Each dr As DataRow In dt.Rows
            For Each col As DataColumn In dt.Columns
                row.Add(col.ColumnName, dr(col))
            Next
            rows.Add(row)
        Next
    Catch ex As Exception
        logFile("SP GetJson ----" + ex.Message)
    End Try
End Sub
于 2013-10-23T14:33:14.677 回答