我想數據庫中的數據轉換成JSON,但我得到通過使用代碼使用JavaScriptSerializer這個網站是這樣轉換數據庫數據轉換成JSON
[{"Yes":6,"No":1,"Maybe":4}].
我已經命名爲Yes
三列的數據庫表, No
和Maybe
,但是我要像一個JSON格式:
[{"name":"Yes","data":6},{"name":"No","data":2},{"name":"Maybe","data":3}].
指如何將它轉換成鍵:值對VB?
我的代碼是:
Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
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
json_data = serializer.Serialize(rows)
我收到錯誤「已經添加了具有相同密鑰的項目」。它不允許再次使用相同密鑰插入數據 –