2013-10-23 78 views
1

我在ASP.NET这一功能的DataTable以JSON字符串转换:转换的数据表列表,以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字符串,任何想法?

+0

您是否尝试过合并'DataTable's成一个,然后调用从'DataTable'到JSON逻辑的转换修改? –

+0

@KarlAnderson实际上,如果我将2个'datatable'合并到1'datatable'中,我将只有一个数组在json字符串中..但实际上我需要在2个根标签的json字符串中有2个数组.. – User7291

+0

@KarlAnderson换句话说,我有2个数据表,我需要将它们转换为一个json字符串,但我需要在同一个字符串中的json数组中的每个数据表 – User7291

回答

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) 

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 
+0

与其他答案相同的错误。'具有相同密钥的项目已被添加。' – User7291

+0

我创建了完整的示例。 – IvanH

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 
+0

我得到了一个错误...'一个项目使用了相同的密钥已被添加' – User7291

+0

我编辑了我的问题,我添加了我需要的json格式检查它 – User7291