Click here to Skip to main content
15,887,923 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

VB
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

VB
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

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:
VB.NET
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
VB.NET
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
 
Share this answer
 
Comments
jonatasbm 19-May-20 10:14am    
Thank you very much for your support
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
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900