Back
Get Json String From URL

Author: Admin 03/25/2021
Language: JSON


Description:

Get Json string from url and convert to DataTable.

Article:

This code example shows how to get a JSON string from URL and convert to DataTable.

'Get Json string
Using json = New WebClient()
       Dim JS As String = json.DownloadString("URL Here")

       gvCFS_Data.DataSource = GetDataTableFromJsonString(JS)
       gvCFS_Data.DataBind()
End Using



'Convert Json string to DataTable
Public Function GetDataTableFromJsonString(json As String) As DataTable
        Dim jsonLinq = JObject.Parse(json)

        ' Find the first array using Linq
        Dim srcArray = jsonLinq.Descendants().Where(Function(d) TypeOf d Is JArray).First()
        Dim trgArray = New JArray()
        For Each row As JObject In srcArray.Children(Of JObject)()
            Dim cleanRow = New JObject()
            For Each column As JProperty In row.Properties()
                ' Only include JValue types
                If TypeOf column.Value Is JValue Then
                    cleanRow.Add(column.Name, column.Value)
                End If
            Next
            trgArray.Add(cleanRow)
        Next

        Return JsonConvert.DeserializeObject(Of DataTable)(trgArray.ToString())
End Function


Back
Comments
Add Comment
There are no comments yet.