2012-04-03 14 views
1

JSONデータをWebからアプリケーションに取り込む際に問題が発生しました。私は "featuredReleases"配列から名前とイメージを取得しようとしています。ここではJSONの一部:あなたは(その本当に長い)フルJSONを確認する必要がある場合はWP7 NullReferenceException JSON配列のデシリアライズ

 "featuredReleases":[ 
    { 
     "id":860118, 
     "type":"release", 
     "name":"Back In Time", 
     "slug":"back-in-time", 
     "releaseDate":"2012-01-30", 
     "publishDate":"2012-01-30", 
     "exclusive":true, 
     "category":"Release", 
     "description":"Toolroom Records breaks new ground once again courtesy of the legendary drum and bass double act Liquid Kaos who have teamed up with vocalist Kirsty Hawkshaw for Back In Time, a drum and bass master class that will be taking over the airwaves and the clubs. Liquid Kaos need little introduction as owners of the legendary Breakbeat Kaos imprint and an impressive list of accolades between them that includes multiple UK Top 10 singles, a Mobo award and the support of the scenes most influential players Zane Lowe, Fabio & Grooverider, Mistajam and more. The most distinctive voice in dance music and a number 1 selling artist in her own right, Kirsty Hawkshaw completes this dream collaboration. Back In Time hooks you in with the haunting vocals of Kirsty Hawkshaw swirling above searing synths and atmospheric strings before dropping into organic grooves and a delectably warm bass. On the remix tip, Swedish star John Dahlb\u00e4ck provides a four to the floor electro workout, dubsteps rising star Cookie Monsta throws in a big, bass heavy re-rub whilst Toolrooms new wonder kid CaPa and Germanys deep house duo Kruse & N\u00fcrnberg complete a versatile package.", 
     "currentStatus":"New Release", 
     "catalogNumber":"TOOL12902Z", 
     "purchasable":true, 
     "images":{ 
      "small":{ 
       "width":30, 
       "height":30, 
       "url":"http:\/\/geo-media.beatport.com\/items\/imageCatalog\/4000000\/900000\/0\/9000\/500\/70\/4909578.jpg", 
       "secureUrl":"https:\/\/media.beatport.com\/items\/imageCatalog\/4000000\/900000\/0\/9000\/500\/70\/4909578.jpg" 
      }, 
      "medium":{ 
       "width":60, 
       "height":60, 
       "url":"http:\/\/geo-media.beatport.com\/items\/imageCatalog\/4000000\/900000\/0\/9000\/500\/70\/4909579.jpg", 
       "secureUrl":"https:\/\/media.beatport.com\/items\/imageCatalog\/4000000\/900000\/0\/9000\/500\/70\/4909579.jpg" 
      }, 
      "large":{ 
       "width":500, 
       "height":500, 
       "url":"http:\/\/geo-media.beatport.com\/items\/imageCatalog\/4000000\/900000\/0\/9000\/500\/80\/4909580.jpg", 
       "secureUrl":"https:\/\/media.beatport.com\/items\/imageCatalog\/4000000\/900000\/0\/9000\/500\/80\/4909580.jpg" 
      } 
     } 
    }, 

ここJSON Formatterに差し込むためのAPIです。 http://api.beatport.com/catalog/3/beatport/home

ここに私のクラスがあります。

namespace Beatport.Classes 
{ 
    public class NewReleasesCharts //Root Object 
    { 
     public Metadata metadata { get; set; } 
     public ResultHome results = new ResultHome(); 
    public IEnumerator<ResultHome> GetEnumerator() 
    { 
     return this.results.GetEnumerator(); 
    } 
} 

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

    //public List<FeaturedCharts> featuredCharts { get; set; } 
    //public List<TopDownloads> topdownloads { get; set; } 
    //public List<MostPopularReleases> mostPopularReleases { get; set; } 
    //public List<Components> components { get; set; } 

    internal IEnumerator<ResultHome> GetEnumerator() 
    { 
     throw new NotImplementedException(); 
    } 
} 

public class FeaturedReleases 
{ 
    public int id { get; set; } 
    public string type { get; set; } 
    public string name { get; set; } 
    public string slug { get; set; } 
    public ReleaseImage releaseImage { get; set; } 
} 

public class ReleaseImage 
{ 
    public ReleaseSmall releaseSmall { get; set; } 
    public ReleaseMedium releaseMedium { get; set; } 
    public ReleaseLarge releaseLarge { get; set; } 
} 

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

最後に、ここでは(json.net付き)JSONをデシリアライズしてデータを引き出すために、私のハンドラがあります。

はNewReleasesChartsをデシリアライズしようとしたとき、私は今json.net例外を取得しています

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

      foreach (FeaturedReleases release in homeData.results.featuredReleases) 
      { 
       string releaseName = release.name; 
       string img = release.releaseImage.releaseMedium.url; 
       listGenres.Items.Add(releaseName); 
       listGenres.Items.Add(img); 
      } 

     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 

    } 

を更新しました。

Cannot deserialize JSON array (i.e. [1,2,3]) into type 'Beatport.Classes.ResultHome'. The deserialized type must be an array or implement a collection interface like IEnumerable, ICollection or IList. To force JSON arrays to deserialize add the JsonArrayAttribute to the type. Line 1, position 58.

+0

のようなものがちょっと聞こえるべきであるとして、あなたのクラス定義を変更

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

としてデシリアライズ、そう? –

答えて

1

あなたは解決策に非常に近いです。

まず、次に

public class FeaturedReleases 
{ 
    public int id { get; set; } 
    public string type { get; set; } 
    public string name { get; set; } 
    public string slug { get; set; } 
    public ReleaseImage images { get; set; } 
} 

public class ReleaseImage 
{ 
    public ReleaseSmall small { get; set; } 
    public ReleaseMedium medium { get; set; } 
    public ReleaseLarge large { get; set; } 
} 

そして最後に、あなたのループはhomeDataがnullの場合のように、この

foreach (FeaturedReleases release in homeData.results.featuredReleases) 
{ 
    string releaseName = release.name; 
    string img = release.images.medium.url; 
    listGenres.Items.Add(releaseName); 
    listGenres.Items.Add(img); 
} 
+0

また、[http://json2csharp.com/](http://json2csharp.com/)はjsonのクラス生成に役立ちます。 – kazarindn

+0

@ L.B NewReleaseChartsを直列化解除すると、json.netエラーが発生します。 "JSON配列を 'Beatport.Classes.ResultsHome'型に逆シリアル化できません。なぜ起こるのでしょうか? – nos9

+0

@ nos9 [ここ](http://pastebin.com/gaF2rWsc)は、winforms用にコンパイルされた完全なコードです(WP7 )しかし、あなたは簡単にあなたのコードと比較することができます –

関連する問題