解析JSON并循环回复

时间:2014-09-12 13:05:04

标签: c# json

我正在尝试解析以下json代码:http://www.soyoustart.com/fr/js/dedicatedAvailability/availability-data.json

但是我对循环数据感到有点困惑,我无法弄清楚如何让循环进入不同的类并让我到达可用性列表。

我以为它会在mydata中,但这似乎只是一个大块。如何过滤掉它以便我可以遍历所有不同的可用性?

static void Main(string[] args)
{
    DB db = new DB();
    DataTable dtServers = db.GetDataTable("select * from SYS_Servers");
    string htmlCode;
    var json = "";
    using (var webClient = new System.Net.WebClient())
    {
        json = webClient.DownloadString("http://www.soyoustart.com/fr/js/dedicatedAvailability/availability-data.json");
    }

    dynamic myData = JsonConvert.DeserializeObject(json, typeof(object));
    String x = "moo";
}

public class Zone
{
    public string availability { get; set; }
    public string zone { get; set; }
}

public class Availability
{
    public string reference { get; set; }
    public List<Zone> zones { get; set; }
}

public class RootObject
{
    public List<Availability> availability { get; set; }
}

1 个答案:

答案 0 :(得分:0)

试试这个:

    static void Main(string[] args)
    {

        DB db = new DB();
        DataTable dtServers = db.GetDataTable("select * from SYS_Servers");
        string htmlCode;
        var json = "";
        using (var webClient = new System.Net.WebClient())
        {
            json = webClient.DownloadString("http://www.soyoustart.com/fr/js/dedicatedAvailability/availability-data.json");
        }

        RootObject myData = JsonConvert.DeserializeObject(json, typeof(RootObject)) as RootObject;
        foreach (var availability in myData.availability)
        {
            //do something with availability
        }
        String x = "moo";
    }
    public class Zone
    {
        public string availability { get; set; }
        public string zone { get; set; }
    }

    public class Availability
    {
        public string reference { get; set; }
        public Zone[] zones { get; set; }
    }

    public class RootObject
    {
        public Availability[] availability { get; set; }
    }