Konversi dataset ke JSON
Setelah muter2 ketemu juga akhirnya. Biar gampangnya, serialisasi ke JSON menggunakan library JSON.NET, di websitenya ada beberapa versi tergantung versi .net framework yang digunakan. BTW, ini kodenya :
1: Imports Newtonsoft.Json
2: Private Function GetCustomer() As String
3: Dim retVal As String = ""
4: Dim ds As DataSet = DAL.Customer.GetCustomers
5:
6: If ds IsNot Nothing AndAlso ds.Tables(0).Rows.Count > 0 Then
7: Dim custList As New List(Of Hashtable)()
8: For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
9: Dim ht As New Hashtable()
10: Dim row As DataRow = TryCast(ds.Tables(0).Rows(i), DataRow)
11: ht("IDCustomer") = Convert.ToString(row("IDCustomer"))
12: ht("CustomerName") = Convert.ToString(row("CustomerName"))
13: custList.Add(ht)
14: Next
15: retVal = JavaScriptConvert.SerializeObject(custList)
16: End If
17:
18: Return retVal
19: End Function
Output yang dihasilkan adalah sebagai berikut (setelah dirapikan):
1: [
2: {"IDCustomer":"1","CustomerName":"Unyil"},
3: {"IDCustomer":"2","CustomerName":"Cuplis"},
4: {"IDCustomer":"3","CustomerName":"Ableh"}
5: ]
No related posts.