Ciao
ho in merito alla stessa questione ho un piccolo problema:
questo il mio codice
Imports System.IO
Imports System.Net
Imports System.Net.Http
Imports System.Threading.Tasks
Imports Json.NET
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports System.Runtime.Serialization.Json
Partial Class _Default
Inherits System.Web.UI.Page
Async Function ScaricaDatiDaApi() As Task(Of String)
Using client As New HttpClient()
Dim risposta = Await client.GetAsync("https://api.coinmarketcap.com/v1/ticker/ripple/").ConfigureAwait(False)
'Mi assicuro che la richiesta sia andata a buon fine. Altrimenti, viene sollevata un'eccezione
risposta.EnsureSuccessStatusCode()
'Questa è la variabile a cui viene assegnato il contenuto JSON
Dim contenutoJson = Await risposta.Content.ReadAsStringAsync()
Return contenutoJson
End Using
End Function
'JsonConvert.DeserializeObject(Of List(Of Rootobject))(contenutoJson)
'JsonConvert.DeserializeObject(Of MyColor)(<JSON String>)
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim contenutoJson = ScaricaDatiDaApi().Result
Dim myIco As Rootobject = JsonConvert.DeserializeObject(Of Rootobject)(contenutoJson)
Dim nomeico = myIco.Property1.name
Me.Literal1.Text = nomeico
End Sub
End Class
Public Class Rootobject
Public Property Property1() As Class1
End Class
Public Class Class1
Public Property id As String
Public Property name As String
Public Property symbol As String
Public Property rank As String
Public Property price_usd As String
Public Property price_btc As String
Public Property _24h_volume_usd As String
Public Property market_cap_usd As String
Public Property available_supply As String
Public Property total_supply As String
Public Property max_supply As String
Public Property percent_change_1h As String
Public Property percent_change_24h As String
Public Property percent_change_7d As String
Public Property last_updated As String
End Class
la risposta JSON è questa
[
{
"id": "bitcoin",
"name": "Bitcoin",
"symbol": "BTC",
"rank": "1",
"price_usd": "9590.28",
"price_btc": "1.0",
"24h_volume_usd": "7520750000.0",
"market_cap_usd": "161486764134",
"available_supply": "16838587.0",
"total_supply": "16838587.0",
"max_supply": "21000000.0",
"percent_change_1h": "0.02",
"percent_change_24h": "-6.57",
"percent_change_7d": "-15.6",
"last_updated": "1517484568"
}
]
il problema è che mi da questo errore:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Rootobject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
il codice che genera l'errore è:
Dim myIco As Rootobject = JsonConvert.DeserializeObject(Of Rootobject)(contenutoJson)
Come posso risolvere?
Grazie mille in anticipo.
Antonio A.