2022年9月26日 星期一

vbnet Newtonsoft.Json DataTable 互轉

 序列化:

Dim dataSet As dataSet = New DataSet("dataSet")

dataSet.[Namespace] = "NetFrameWork"

Dim table As New DataTable()

Dim idColumn As New DataColumn("id", GetType(Integer))

idColumn.AutoIncrement = True

Dim itemColumn As New DataColumn("item")

table.Columns.Add(idColumn)

table.Columns.Add(itemColumn)

dataSet.Tables.Add(table)

Dim i As Integer = 0

While i < 2

Dim newRow As DataRow = table.NewRow()

newRow("item") = "item " + i

table.Rows.Add(newRow)

i += 1

End While

dataSet.AcceptChanges()

Dim json As String = JsonConvert.SerializeObject(dataSet, Formatting.Indented)


反序列:

Dim json As String = IO.File.ReadAllText(HttpContext.Current.Server.MapPath("~/Data.json"))

Dim dt As New System.Data.DataTable

dt = Newtonsoft.Json.JsonConvert.DeserializeObject(Of DataTable)(json)


沒有留言:

張貼留言