I am getting an error when trying to convert json response from coinmarketcap.com's API.
The error is:
"Unable to cast object of type 'System.Collections.Generic.List`1[System.Object]' to type 'System.Dynamic.ExpandoObject'"
The web client works fine for other API's but for some reason coin object is not populated. Any insight into a fix in either vb.net or c# would be much appreciated.
Dim dt_results As DataTable, dr As DataRow, url As String = String.Empty, json As Object = Nothing, iCount As Integer = 0
'temporarily store results
dt_results = New DataTable
dt_results.Columns.Add("name", GetType(String))
dt_results.Columns.Add("symbol", GetType(String))
dt_results.Columns.Add("price_usd", GetType(Double))
Try
url = "https://api.coinmarketcap.com/v1/ticker/?convert=usd&limit=10"
Dim theurl As New Uri(url)
Using webClient = New System.Net.WebClient()
json = webClient.DownloadString(theurl)
'create json object
Dim converter = New ExpandoObjectConverter()
Dim coin As Object = JsonConvert.DeserializeObject(Of ExpandoObject)(json, converter)
For Each item In coin
Dim name As String = coin(iCount).name
Dim symbol As String = coin(iCount).symbol
Dim price_usd As Double = coin(iCount).price_usd
dr = dt_results.NewRow()
dr("name") = name
dr("symbol") = symbol
dr("price_usd") = price_usd
dt_results.Rows.Add(dr)
iCount = iCount + 1
Next
End Using
Catch ex As Exception
Dim ts As String = ex.Message
json = "1"
End Try
Working solution...
Dim d As JArray = JArray.Parse(json)
For i As Integer = 0 To d.Count
Dim name As String = d(i).Item("name")
Dim symbol As String = d(i).Item("symbol")
Dim price_usd As Double = CDbl(d(i).Item("price_usd"))
dr = dt_results.NewRow()
dr("name") = name
dr("symbol") = symbol
dr("price_usd") = price_usd
dt_results.Rows.Add(dr)
iCount = iCount + 1
Next