2012-02-06 50 views
0

我想從列表中的列表返回json數據。數據正在使用webclient進行提取,並使用JSON.NET進行反序列化。我想從「結果」列表中的「featuredCharts」列表中返回一個名稱和圖像。這裏是我所指的json數據的一部分。WP7 JSON返回列表中的列表

"results":{ 
    "featuredCharts":[ 
    { 
     "id":46082, 
     "type":"chart", 
     "name":"Exclusives On Beatport - Week 5", 
     "slug":"exclusives-on-beatport-week-5", 
     "description":"", 
     "publishDate":"2012-01-30", 
     "price":{ 
      "code":"usd", 
      "symbol":"$", 
      "value":2390 
     }, 
     "audioFormatFee":{ 
      "wav":{ 
       "code":"usd", 
       "symbol":"$", 
       "value":1000 
      }, 
      "aiff":{ 
       "code":"usd", 
       "symbol":"$", 
       "value":1000 
      } 
     }, 
     "genres":[ 
      { 
       "id":11, 
       "name":"Tech House", 
       "slug":"tech-house", 
       "type":"genre" 
      }, 
      { 
       "id":5, 
       "name":"House", 
       "slug":"house", 
       "type":"genre" 
      }, 
      { 
       "id":17, 
       "name":"Electro House", 
       "slug":"electro-house", 
       "type":"genre" 
      }, 
      { 
       "id":15, 
       "name":"Progressive House", 
       "slug":"progressive-house", 
       "type":"genre" 
      } 
     ], 
     "images":{ 
      "small":{ 
       "width":30, 
       "height":30, 
       "url":"http:\/\/geo-media.beatport.com\/items\/imageCatalog\/4000000\/900000\/50000\/1000\/200\/40\/4951247.jpg", 
       "secureUrl":"https:\/\/media.beatport.com\/items\/imageCatalog\/4000000\/900000\/50000\/1000\/200\/40\/4951247.jpg" 
      }, 
      "medium":{ 
       "width":60, 
       "height":60, 
       "url":"http:\/\/geo-media.beatport.com\/items\/imageCatalog\/4000000\/900000\/50000\/1000\/200\/40\/4951248.jpg", 
       "secureUrl":"https:\/\/media.beatport.com\/items\/imageCatalog\/4000000\/900000\/50000\/1000\/200\/40\/4951248.jpg" 
      }, 
      "large":{ 
       "width":130, 
       "height":130, 
       "url":"http:\/\/geo-media.beatport.com\/items\/imageCatalog\/4000000\/900000\/50000\/1000\/200\/40\/4951249.jpg", 
       "secureUrl":"https:\/\/media.beatport.com\/items\/imageCatalog\/4000000\/900000\/50000\/1000\/200\/40\/4951249.jpg" 
      }, 
      "xlarge":{ 
       "width":500, 
       "height":500, 
       "url":"http:\/\/geo-media.beatport.com\/items\/imageCatalog\/4000000\/900000\/50000\/1000\/200\/50\/4951250.jpg", 
       "secureUrl":"https:\/\/media.beatport.com\/items\/imageCatalog\/4000000\/900000\/50000\/1000\/200\/50\/4951250.jpg" 
      } 
     }, 
     "chartOwner":null 
    }, 

我的課程目前正在像這樣安裝。

public class NewReleasesCharts //Root Object 
{ 
    public Metadata metadata { get; set; } 
    public List<ResultHome> results = new List<ResultHome>(); 

    public IEnumerator<ResultHome> GetEnumerator() 
    { 
     return this.results.GetEnumerator(); 
    } 
} 

public class ResultHome 
{ 
    public List<FeaturedCharts> featuredCharts { get; set; } 
    public List<FeaturedReleases> featuredReleases { get; set; } 
} 

public class FeaturedCharts 
{ 
    public int id { get; set; } 
    public string type { get; set; } 
    public string name { get; set; } 
    public string slug { get; set; } 
    public ChartImages chartImages { get; set; } 
} 

public class ChartImages 
{ 
    public ChartSmall chartSmall { get; set; } 
    public ChartMedium chartMedium { get; set; } 
    public ChartLarge chartLarge { get; set; } 
} 

public class ChartMedium 
{ 
    public int width { get; set; } 
    public int height { get; set; } 
    public string url { get; set; } 
    public string secureUrl { get; set; } 
} 

這是我被卡住的部分。反序列化我想要使用嵌套的foreach循環的數據後,但我目前得到一個錯誤「無法將類型Beatport.Classes.ResultHome轉換爲」Beatport.Classes.FeaturedCharts「。這是代碼。

UPDATE我更新了我的代碼,每個ColinE的答案,我現在得到一個NullReferenceException錯誤的內部foreach循環。

// Deserialize home page data 
    void jsonHome_GetDataCompleted(object snder, DownloadStringCompletedEventArgs e) 
    { 
     try 
     { 
      NewReleasesCharts homeData = JsonConvert.DeserializeObject<NewReleasesCharts>(e.Result); 

      try 
      { 
       // Nested foreach loops to dispaly data 
       foreach (ResultHome rc in homeData) 
       { 
        try 
        { 
         foreach (FeaturedCharts fc in rc.featuredCharts) 
         { 
          // TODO: return name and image of chart 
          string name = fc.name; 
          listCharts.Items.Add(name); 
         } 
        } 
        catch (Exception ex) 
        { 
         MessageBox.Show(ex.Message); 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

我還是一個初學者,當談到C#所以我不知道如果嵌套的foreach循環是正確的路要走。如何正確地做到這一點的例子會給我一些東西來構建,因爲我需要在我的應用程序的其他部分執行此操作。

謝謝。

回答

0

你的第二個循環是迭代與第一個相同的數據。您需要遍歷變量作爲一個在外環迭代的featuredCharts屬性:

try 
    { 
     NewReleasesCharts homeData = JsonConvert.DeserializeObject<NewReleasesCharts>(e.Result); 

     // foreach loop to dispaly data 
     foreach (ResultHome rc in homeData) 
     { 
      foreach (FeaturedCharts fc in rc.featuredCharts) 
      { 
       // return name and image of chart 
      } 
     } 
    } 

爲了解決這類問題的特點,嘗試在你的代碼中設置斷點,並在調試模式下運行。然後,您可以檢查每個變量的類型,以確定您做錯了什麼。

+0

感謝您的回答科林。使用你的例子,我在內部的foreach循環中得到一個NullReferenceException。查看更新以獲取新代碼。 – nos9 2012-02-07 02:06:33