将数据表的列表转换为json

时间:2013-10-23 14:15:57

标签: asp.net json vb.net datatable vb.net-2010

我在ASP.NET中使用此函数将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"
    }
]
}

但现在我有一个2 DataTable的列表,我需要将它转换为一个包含2个数组的JSON字符串,任何想法?

2 个答案:

答案 0 :(得分: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字符串的结构。现在我创建表(它们在DataSet中并且名称以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

答案 1 :(得分: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