WP7JSON返回列表中的列表



我试图从列表中的列表返回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循环,但我目前收到一个错误"无法将类型Beatport.Classes.ResultHome转换为Beatport.Classes.FeaturedCharts"。这是代码。

UPDATE我根据ColinE的回答更新了代码,现在在foreach内部循环中出现NullReferenceException错误。

    // 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循环是否是正确的方法。一个如何正确地做到这一点的例子会给我一些可以借鉴的东西,因为我需要在我的应用程序的其他部分中这样做。

谢谢。

您的第二个for循环正在迭代与第一个相同的数据。您需要在外循环中迭代的变量的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
            }
        }
    }

要解决该功能中的此类问题,请尝试在代码中设置断点并在调试模式下运行。然后,您可以检查每个变量的类型,以确定您做错了什么。

相关内容

  • 没有找到相关文章

最新更新