Vikrant Vikrant - 1 month ago 14
JSON Question

convert system.Object[] to Datatable in VB.NET

I m trying to Deserialize

Json
string and convert it to
DataTable
, using
JsonConvert.DeserializeObject
.

VB Function:



Public Function DerializeDataTable(ByVal data As String) As DataTable
Dim json As New JavaScriptSerializer
Dim j As Object = json.DeserializeObject(data)
Return j
End Function


Exception:



Unable to cast object of type 'System.Object[]' to type 'System.Data.DataTable'.

How to Deserialized
Json
string to
datatable
?

Mez Mez
Answer

You can use the Json.NET framework. Then you can use the following code in your case

var table = JsonConvert.DeserializeObject<DataTable>(json);
return table;

For vb.net

Dim dt as DataTable = Json.JsonConvert.DeserializeObject(Of DataTable)(data)

So your complete function would be

Public Function DerializeDataTable(ByVal data As String) As DataTable 
    Dim j As DataTable = Json.JsonConvert.DeserializeObject(Of DataTable)(data)
    Return j
End Function
Comments