将json中的嵌套类反序列化为c#对象
我正在尝试从中反序列化JSON。我不能使用全局类的属性将json中的嵌套类反序列化为c#对象,c#,json,C#,Json,我正在尝试从中反序列化JSON。我不能使用全局类的属性 namespace CoVID2 { public class CovidStats { public string ID { get; set; } public string Message { get; set; } public class Global { public int NewConfirmed { get; set; }
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
}
}
名称空间CoVID2
{
公共类共变病毒
{
公共字符串ID{get;set;}
公共字符串消息{get;set;}
公共类全球{
public int{get;set;}
公共整数{get;set;}
公共整数{get;set;}
公共整数{get;set;}
public int{get;set;}
公共整数{get;set;}
公共字符串日期{get;set;}
}
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
使用(WebClient=newWebClient())
{
string s=client.DownloadString(url);
CovidStats stat=JsonConvert.DeserializeObject;
MessageBox.Show(stat.Global.totalconfirm.ToString());//这就是我得到错误的地方
}
}
错误CS0572“全局”:无法通过表达式引用类型;请尝试使用“CovidStats.Global”而不是CoVID2 C:\Users\Yan\source\repos\CoVID2\CoVID2\Form1.cs 64 Active
错误CS0120非静态字段、方法或属性“CovidStats.Global.TotalConfirmed”CoVID2 C:\Users\Yan\source\repos\CoVID2\CoVID2\Form1.cs 64 Active”需要对象引用。您可以使用它轻松地将有效的JSON字符串转换为C类
基于来自的JSON,我得到了以下结果:
// 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; }
}
//根myDeserializedClass=JsonConvert.DeserializeObject(myJsonResponse);
公共类全球
{
public int{get;set;}
公共整数{get;set;}
公共整数{get;set;}
公共整数{get;set;}
public int{get;set;}
公共整数{get;set;}
公共日期时间日期{get;set;}
}
公务舱保费
{
}
公营国家
{
公共字符串ID{get;set;}
公共字符串国家{get;set;}
公共字符串CountryCode{get;set;}
公共字符串Slug{get;set;}
public int{get;set;}
公共整数{get;set;}
公共整数{get;set;}
公共整数{get;set;}
public int{get;set;}
公共整数{get;set;}
公共日期时间日期{get;set;}
公共溢价{get;set;}
}
公共类根
{
公共字符串ID{get;set;}
公共字符串消息{get;set;}
公共全局{get;set;}
公共列表国家{get;set;}
公共日期时间日期{get;set;}
}
这是否回答了您的问题?