嵌套foreach循环的NullReferenceException WP7
本文关键字:NullReferenceException WP7 循环 foreach 嵌套 | 更新日期: 2023-09-27 18:24:41
我试图从列表中的列表返回json数据。数据是使用Web客户端提取的,并使用JSON.NET进行反序列化。我想从"结果"列表中的"featuredCharts"列表中返回一个名称和图像。这是我所指的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循环,但我目前在内部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#,我还是个初学者,所以我不确定自己做错了什么。一个如何正确地做到这一点的例子会给我一些可以借鉴的东西,因为我需要在我的应用程序的其他部分中这样做。
谢谢。
更新以下是完整的例外:
System.NullReferenceException was unhandled
消息=NullReferenceExceptionStackTrace:位于Beatport.MainPage.jsonHome_GetDataCompleted(Object snder,DownloadStringCompletedEventArgs e)位于System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs e)位于System.Net.WebClient.DownloadStringOperationCompleted(对象arg)位于System.Reflection.RuntimeMethodInfo.InteralInvoke(RuntimeMethodInfo rtmi,Object obj,BindingFlags invokeAttr,Binder Binder,Object parameters,CultureInfo区域性,Boolean isBinderDefault,Assembly调用者,Boolean verifyAccess,StackCrawlMark&stackMark)位于System.Reflection.RuntimeMethodInfo.InteralInvoke(Object obj,BindingFlags invokeAttr,Binder Binder,Object[]参数,CultureInfo区域性,StackCrawlMark&stackMark)位于System.Reflection.MethodBase.Invoke(Object obj,Object[]参数)位于System.Delegate.DynamicInvokeOne(Object[]args)位于System.MulticastDelegate.DynamicInvokeImpl(Object[]args)位于System.Delegate.DynamicInvoke(Object[]args)位于System.Windows.Threading.DispatcherOperation.IInvoke()位于System.Windows.Threading.Dispatcher.Dispatcher(DispatcherPriority优先级)位于System.Windows.Threading.Dispatcher.OnInvoke(对象上下文)位于System.Windows.Hosting.CallbackCookie.Invoke(Object[]args)位于System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[]args)在System.Windows.RuntimeHost.ManagedHost.IInvokeDelegate(IntPtr pHandle、Int32 nParamCount、ScriptParam[]pParams、ScriptParam和pResult)
很难说,因为您发布的json对象看起来不错,但并不完整。
从这里开始:http://json2csharp.com/
将生成的类与您自己的类进行比较。
如果异常发生在string name = fc.name;
行,那么fc
必须为null,尽管这看起来不太可能。