Click here to Skip to main content
14,545,256 members
Rate this:
Please Sign up or sign in to vote.
See more:
Problems when trying to collect data from a Json Web Service

What I have tried:

Hello good morning everyone, Sorry for my bad English.

I'm trying to collect data from this Api

https://olinda.bcb.gov.br/olinda/servico/PTAX/versao/v1/odata/CotacaoDolarDia(dataCotacao=@dataCotacao)?@dataCotacao=%2705-05-2020%27&$format=json&$select=cotacaoVenda[^]

She returns these values ​​to me as a result

{"@odata.context":"https://was-p.bcnet.bcb.gov.br/olinda/servico/PTAX/versao/v1/odata$metadata#_CotacaoDolarDia(cotacaoVenda)","value":[{"cotacaoVenda":5.53540}]}

I need to get this value
5.53540
But I can't, it always returns me empty or 0

my code

Private Async Sub Teste()
        Dim Uri As String

        Uri = "https://olinda.bcb.gov.br/olinda/servico/PTAX/versao/v1/odata/CotacaoDolarDia(dataCotacao=@dataCotacao)?@dataCotacao=%2705-05-2020%27&$format=json&$select=cotacaoVenda"
        Using client = New HttpClient()
            Using response = Await client.GetAsync(Uri)
                If response.IsSuccessStatusCode Then
                    Dim ProdutoJsonString = Await response.Content.ReadAsStringAsync()
                    Dim list = JsonConvert.DeserializeObject(Of value)(ProdutoJsonString)
                    MsgBox(list.cotacaoVenda)

                Else
                    MessageBox.Show("Não foi possível obter o produto : " + response.StatusCode)
                End If
            End Using
        End Using
    End Sub

Public Class value

    Public Property cotacaoVenda As Decimal

End Class


What can I be doing wrong?
Thank you all in advance.
Jonatas Marques
Posted
Updated 19-May-20 4:04am
Rate this:
Please Sign up or sign in to vote.

Solution 1

The value you're trying to deserialize doesn't match the JSON you've shown.

To deserialize that JSON, your classes need to look like:
Public Class Root
    <JsonProperty("@odata.context")> _
    Public Property OdataContext As Uri

    <JsonProperty("value")> _
    Public Property Value As Value()
End Class

Public Class Value
    <JsonProperty("cotacaoVenda")> _
    Public Property CotacaoVenda As Decimal
End Class
Dim list As Root = JsonConvert.DeserializeObject(Of Root)(ProdutoJsonString)

If list Is Nothing OrElse list.Value Is Nothing OrElse list.Value.Length = 0 Then
    MsgBox("Invalid JSON data returned")
Else
    MsgBox(list.Value(0).cotacaoVenda)
End
   
Comments
jonatasbm 19-May-20 10:14am
   
Thank you very much for your support
Rate this:
Please Sign up or sign in to vote.

Solution 2

If I pass your code to a Class creator (such as JSON Utils: Generate C#, VB.Net, SQL Table, Java and PHP from JSON[^]) I dont; get such a limited class, as I'd expect:
Public Class Value
       Public Property cotacaoVenda As Double
   End Class

   Public Class Example
       Public Property @odata.context As String
       Public Property value As Value()
   End Class

And then accessing list.value(0).cotacaoVenda gives me 5.5354
   
Comments
jonatasbm 19-May-20 10:14am
   
Thank you very much for your support
OriginalGriff 19-May-20 10:22am
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100