Ho preparato questo codice che, al click di un pulsante, dovrebbe restituire dei dati:
protected void Button1_Click(object sender, EventArgs e)
{
try
{
var request = HttpWebRequest.Create("https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-json/dpc-covid19-ita-andamento-nazionale.json");
request.ContentType = "text/json";
request.Method = "GET";
var httpResponse = (HttpWebResponse)request.GetResponse();
var StreamReader = new StreamReader(httpResponse.GetResponseStream());
var responseText = StreamReader.ReadToEnd();
CovidClass covidClass = JsonConvert.DeserializeObject<CovidClass>(responseText);
int deceduti = covidClass.deceduti;
lblStatus.Text = "Totale deceduti: " + deceduti.ToString();
}
catch (Exception exc)
{
lblStatus.Text = exc.Message.ToString();
}
}
Classe:
public class CovidClass
{
public DateTime data { get; set; }
public string stato { get; set; }
public int ricoverati_con_sintomi { get; set; }
public int terapia_intensiva { get; set; }
public int totale_ospedalizzati { get; set; }
public int isolamento_domiciliare { get; set; }
public int totale_attualmente_positivi { get; set; }
public int nuovi_attualmente_positivi { get; set; }
public int dimessi_guariti { get; set; }
public int deceduti { get; set; }
public int totale_casi { get; set; }
public int tamponi { get; set; }
public string note_it { get; set; }
public string note_en { get; set; }
}
Ma al click mi viene fuori questo errore:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Study.CovidClass' 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 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. Path '', line 1, position 1.