2014-09-12 69 views
0

我試圖解析以下JSON代碼:但是我越來越有點糊塗了通過數據的循環http://www.soyoustart.com/fr/js/dedicatedAvailability/availability-data.json解析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; } 
} 

回答

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; } 
    } 
+0

啊真的很有幫助。我如何通過RootObject循環? – TMB87 2014-09-12 13:40:55

+0

只需使用'for'或'foreach'? – 2014-09-12 13:42:33

+0

對不起,我明白了,但我循環了什麼?一個rootobject? – TMB87 2014-09-12 14:04:44