循环遍历嵌套的类对象

时间:2017-12-11 15:37:39

标签: c#

我已经从JSON提要(SendGrid API类别统计信息)生成了这些类:

public class Metrics
{
    public int blocks { get; set; }
    public int bounce_drops { get; set; }
    public int bounces { get; set; }
    public int clicks { get; set; }
    public int deferred { get; set; }
    public int delivered { get; set; }
    public int invalid_emails { get; set; }
    public int opens { get; set; }
    public int processed { get; set; }
    public int requests { get; set; }
    public int spam_report_drops { get; set; }
    public int spam_reports { get; set; }
    public int unique_clicks { get; set; }
    public int unique_opens { get; set; }
    public int unsubscribe_drops { get; set; }
    public int unsubscribes { get; set; }
}

public class Stat
{
    public string type { get; set; }
    public string name { get; set; }
    public Metrics metrics { get; set; }
}

public class RootObject
{
    public string date { get; set; }
    public List<Stat> stats { get; set; }
}

填写如下:

RootObject Datalist = JsonConvert.DeserializeObject<RootObject>(jsonString);

我想遍历数据以显示它并保存到数据库。我可以按如下方式遍历RootObject:

Type type = Datalist.GetType();
PropertyInfo[] properties = type.GetProperties();

foreach (PropertyInfo property in properties)
{
    Response.Write("Name: " + property.Name + ", Value: " + property.GetValue(Datalist, null) + "<br>");
}

但我该如何循环:

  • RootObject
    • 统计
      • 度量

0 个答案:

没有答案