I'm trying to deserialize a JSON from here . And I can't use ie properties from class Global.
namespace CoVID2
{
public class CovidStats
{
public string ID { get; set; }
public string Message { get; set; }
public class Global {
public int NewConfirmed { get; set; }
public int TotalConfirmed { get; set; }
public int NewDeaths { get; set; }
public int TotalDeaths { get; set; }
public int NewRecovered { get; set; }
public int TotalRecovered { get; set; }
public string Date { get; set; }
}
}
private void button1_Click(object sender, EventArgs e)
{
using (WebClient client = new WebClient())
{
string s = client.DownloadString(url);
CovidStats stat = JsonConvert.DeserializeObject<CovidStats>(s);
MessageBox.Show(stat.Global.TotalConfirmed.ToString()); // This is the place, where i get an error
}
}
Error CS0572 'Global': cannot reference a type through an expression; try 'CovidStats.Global' instead CoVID2 C:\Users\Yan\source\repos\CoVID2\CoVID2\Form1.cs 64 Active Error CS0120 An object reference is required for the non-static field, method, or property 'CovidStats.Global.TotalConfirmed' CoVID2 C:\Users\Yan\source\repos\CoVID2\CoVID2\Form1.cs 64 Active
You can use https://json2csharp.com/ to easily convert valid JSON string into C# classes.
Based on the JSON from https://api.covid19api.com/summary I got the following result:
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Global
{
public int NewConfirmed { get; set; }
public int TotalConfirmed { get; set; }
public int NewDeaths { get; set; }
public int TotalDeaths { get; set; }
public int NewRecovered { get; set; }
public int TotalRecovered { get; set; }
public DateTime Date { get; set; }
}
public class Premium
{
}
public class Country
{
public string ID { get; set; }
public string Country { get; set; }
public string CountryCode { get; set; }
public string Slug { get; set; }
public int NewConfirmed { get; set; }
public int TotalConfirmed { get; set; }
public int NewDeaths { get; set; }
public int TotalDeaths { get; set; }
public int NewRecovered { get; set; }
public int TotalRecovered { get; set; }
public DateTime Date { get; set; }
public Premium Premium { get; set; }
}
public class Root
{
public string ID { get; set; }
public string Message { get; set; }
public Global Global { get; set; }
public List<Country> Countries { get; set; }
public DateTime Date { get; set; }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.