1

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

1 Answer 1

1

Haven't written in VB.NET for a very long while, but I have a solution for you in C#. If you do not have a clearly defined object model, I would recommend using Json.Net, parsing it as a dynamic object. We're getting and array from the API, so we need to tell Json.Net that we're expecting it to parse it as such. Below is a working solution for me (albeit in C#)

using (WebClient cli = new WebClient())
        {
            string result = cli.DownloadString("https://api.coinmarketcap.com/v1/ticker/?convert=usd&limit=10");
            dynamic arrayFromApi = JArray.Parse(result);

            // Use however - presumably loop through items
            string s = arrayFromApi[0].name;
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Glad I could help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.