2012-04-03 4 views
1

J'ai eu du mal à extraire des données JSON du Web dans mon application. J'essaye de tirer un nom et une image du tableau "featuredReleases". Voici une partie du JSON:WP7 NullReferenceException désérialisation du tableau 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" 
      } 
     } 
    }, 

Si vous avez besoin de voir la pleine JSON (sa très longue) voici le api pour brancher un JSON Formatter. http://api.beatport.com/catalog/3/beatport/home

Voici mes cours. Enfin, voici mon gestionnaire pour désérialiser le JSON (avec json.net) et extraire les données.

MISE À JOUR

// 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); 
     } 

    } 

Je reçois maintenant une exception json.net lorsque vous essayez de désérialiser NewReleasesCharts.

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

sons Un peu comme homeData est nulle n'est-ce pas? –

Répondre

1

Vous êtes très proche de la solution.

D'abord, désérialiser comme

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

Ensuite, changez vos définitions de classe

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; } 
} 

et enfin, votre boucle doit être quelque chose comme ça

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

Aussi [http://json2csharp.com/](http://json2csharp.com/) pourrait être utile dans la génération de classe de json. – kazarindn

+0

@ L.B Deserializing NewReleaseCharts provoque une erreur json.net. "Impossible de désérialiser le tableau JSON dans le type" Beatport.Classes.ResultsHome " – nos9

+0

@ nos9 [Ici] (http://pastebin.com/gaF2rWsc) est un code entièrement fonctionnel compilé pour les winforms (pas WP7), mais vous pouvez facilement le comparer avec votre code –