2012-02-16 115 views
0

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

"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循環,但我目前在內部foreach循環中rc.featuredCharts NullReferenceException錯誤。這是代碼。

// Deserialize home page data 
    void jsonHome_GetDataCompleted(object snder, DownloadStringCompletedEventArgs e) 
    { 
     NewReleasesCharts homeData = JsonConvert.DeserializeObject<NewReleasesCharts>(e.Result); 
     try 
     { 
      // Nested foreach loops to dispaly data 
      foreach (ResultHome rc in homeData) 
      { 
       foreach (FeaturedCharts fc in rc.featuredCharts) 
       { 
        // TODO: return name and image of chart 
        string name = fc.name; 
        listGenres.Items.Add(name); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString()); 
     } 
    } 

我仍然是初學者,當談到C#,所以我不知道我在做什麼錯了。如何正確地做到這一點的例子會給我一些東西來構建,因爲我需要在我的應用程序的其他部分執行此操作。

謝謝。

UPDATE以下是完整的例外:

System.NullReferenceException was unhandled 

消息=的NullReferenceException 堆棧跟蹤: 在Beatport.MainPage.jsonHome_GetDataCompleted(對象snder,DownloadStringCompletedEventArgs E) 在System.Net.WebClient.OnDownloadStringCompleted( DownloadStringCompletedEventArgs e) at System.Net.WebClient.DownloadStringOperationCompleted(Object arg) at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi,Object obj,BindingFlags invokeAttr ,Binder binder,Object參數,CultureInfo culture,布爾isBinderDefault,Assembly調用方,布爾verifyAccess,StackCrawlMark & stackMark) at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,BindingFlags invokeAttr,Binder binder,Object [] parameters,CultureInfo culture, StackCrawlMark & stackMark) 在System.Reflection.MethodBase.Invoke(對象OBJ,在System.Delegate.DynamicInvokeOne(對象[]參數) 對象[]參數) 在System.MulticastDelegate.DynamicInvokeImpl(對象[]參數) 在System.Delegate.DynamicInvoke(Object [] args) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)System.Windows.Threading.Dispatcher.OnInvoke(Object [] args) (Object [] args) 在System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr的pHandle,的Int32 nParamCount,ScriptParam [] pParams,ScriptParam & pResult)

+0

可能重複[什麼是.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – 2012-02-16 02:55:12

+0

另外,我建議你離開在任何地方都使用try/catch塊的習慣。通常最好允許異常在堆棧中儘可能高地傳播。所以_maybe_你的代碼應該有一個外層的'try/catch/MessageBox.Show(ex.ToString());'注意ex.Message只輸出消息文本,並且你會想顯示完整的異常。 – 2012-02-16 02:57:06

+0

很難說只看代碼,但如果你在'foreach(rC.featuredCharts中的FeaturedCharts fc)'設置了一個斷點'''rc.featuredCharts'的內容是什麼? – Alan 2012-02-16 02:59:23

回答

0

這很難說,因爲您發佈的JSON對象看起來不錯,但它是不完整的。

從這裏開始:http://json2csharp.com/

比較類,生成你自己。

+0

我已經嘗試過,但由於某種原因,它不適用於此特定的json字符串。我在自己的 – nos9 2012-02-16 03:33:32

0

如果線路string name = fc.name;發生異常,那麼fc必須爲空,就像看起來似乎不太可能。

+0

上構建了類,在'rc.featuredCharts'上發生了異常。它永遠不會到達內部foreach循環內的代碼。 – nos9 2012-02-16 05:42:10

+0

然後rc.featuredCharts爲空。 – 2012-02-16 05:49:19