Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to deal with changing element names when de-serializing JSON to C#
    primarykey
    data
    text
    <p>This is a follow on from <a href="https://stackoverflow.com/questions/17454055/dealing-with-fanart-tv-webservice-response-json-and-c-sharp">Dealing with fanart.tv webservice response JSON and C#</a></p> <p>So for artists you get a response where the main element is named the same as the artist (hence you call the webservice for thumbs for Metallica the root it called Metallica, you call it for Megadeth is is called Megadeth)</p> <p>The previous question resolves that but the next stage down is albums which ends up containing two levels of these dynamically named elements so searching for a particular album and you get</p> <pre><code>{"Metallica":{"mbid_id":"65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab","albums":{"f44f4f73-a714-31a1-a4b8-bfcaaf311f50":{"albumcover":[{"id":"51252","url":"http://assets.fanart.tv/fanart/music/65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab/albumcover/kill-em-all-504c485914b25.jpg","likes":"2"}]}}}} </code></pre> <p>So you have the same top level artist details with the dynamic name but now you have for albums you have an element which is named after the MusicbrainzID and then the collection of thumbs.</p> <p>For the artist details I defined classes</p> <pre><code>public class ArtistThumbs { public string Name { get; set; } [JsonProperty("mbid_id")] public string MusicBrainzID { get; set; } [JsonProperty("artistthumb")] public List&lt;Thumb&gt; Thumbs { get; set; } } </code></pre> <p>and</p> <pre><code>public class Thumb { [JsonProperty("id")] public string Id { get; set; } [JsonProperty("url")] public string Url { get; set; } [JsonProperty("likes")] public string Likes { get; set; } } </code></pre> <p>Then I can just de-serialize the artist to JSON with</p> <pre><code>var artistthumbs = JsonConvert.DeserializeObject&lt;Dictionary&lt;string, Artist&gt;&gt;(json); </code></pre> <p>question is how can I do the same for these albums? I really need to try and stick with <code>JsonConvert.DeserializeObject</code> if possible and really want to de-serialize to classes but I can't figure out how to model the classes</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload