【问题标题】:convert a list of datatable to json将数据表列表转换为 json
【发布时间】:2013-10-23 14:15:56
【问题描述】:

我在 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 个DataTables 的列表,我需要将其转换为一个包含 2 个数组的 JSON 字符串,有什么想法吗?

【问题讨论】:

  • 您是否尝试过将DataTables 合并为一个,然后调用从DataTable 到JSON 逻辑的转换?
  • @KarlAnderson 实际上如果我将 2 个 datatables 合并为 1 个 datatable 我将在 json 字符串中只有一个数组.. 但实际上我需要在 json 中有 2 个数组带有 2 个根标签的字符串..
  • @KarlAnderson 换句话说我有 2 个数据表,我需要将它们转换为一个 json 字符串,但我需要 json 数组中的每个数据表在同一个字符串中
  • 您能添加一个您想要实现的 JSON 字符串的简短示例吗?
  • @IvanH 我编辑了我的问题并添加了我需要的 json 格式

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


【解决方案1】:

您正在寻找的结构是现在创建的列表字典。 尝试反序列化你的结构(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)

GetRowsGetJson修改

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

【讨论】:

  • 与其他答案相同的错误.. An item with the same key has already been added.
  • 我已经创建了完整的例子。
【解决方案2】:
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

【讨论】:

  • 我出错了...an item with the same key has been added
  • 我编辑了我的问题并添加了我需要的 json 格式,请检查它
猜你喜欢
  • 2020-05-15
  • 2019-02-08
  • 2023-02-23
  • 2020-09-02
  • 2012-01-01
  • 2021-02-09
  • 2017-01-24
  • 2012-08-12
相关资源
最近更新 更多