0

I'm trying to deserialize a JSON from here. And I can't use i.e. 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

  • Does this answer your question? [CS0120: An object reference is required for the nonstatic field, method, or property 'foo'](https://stackoverflow.com/questions/498400/cs0120-an-object-reference-is-required-for-the-nonstatic-field-method-or-prop) – Yong Shun Apr 29 '21 at 08:41

1 Answers1

0

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; }
}
jegtugado
  • 5,081
  • 1
  • 12
  • 35