Take the 2-minute tour ×

I am trying to use the fanart.tv webservice API but have a couple of issues. I am using Json.Net (Newtonsoft.Json) and with other web-services I have de-serialized the JSON reponses into C# objects directly.

The issue here is that the element names are changing. Eg. if I search for artist thumbs for Metallica you get

{"Metallica":{"mbid_id":"65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab","artistthumb":        [{"id":"36181","url":"http://assets.fanart.tv/fanart/music/65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab/artistthumb/metallica-4fd83b0129f83.jpg","likes":"1"},...]}}

So the root element is called Metallica. Obviously search for another artist and you get a different element name. After attempting to deserialize this to an object I gave up and as in reality all I needed was a list of strings (the urls) I tried to process the JSON

  var obj = _downloader.Download<JObject>(url);
  if (obj != null)
    if (obj.HasValues)
      var fanartArtist = (JProperty)obj.First;
      if (fanartArtist.HasValues)
        var thumbs = fanartArtist.Value[SearchSubTypeToString(subType)];
        if (thumbs.HasValues)
          thumbUrls.AddRange(thumbs.Select(thumb => thumb["url"].ToString()));

which works fine when there is a response but if there are no thumbs the web-service returns null and this code fails with

Unable to cast object of type 'Newtonsoft.Json.Linq.JValue' to type 'Newtonsoft.Json.Linq.JObject'.

To complicate matters slightly I am sort of limited by the application and ideally I need to use


So the question is what what is the best approach to solve both of these issues?

share|improve this question
The data you're getting actually consists of a dictionary with a single key: Metallica, with a value that has all the rest of the data in it. –  Bobson Jul 3 '13 at 17:08

1 Answer 1

up vote 1 down vote accepted

Try this:

First, define objects to hold the data deserialized from the JSON:

class Artist
    public Guid mb_id { get; set; }
    public List<Thumb> artistthumb { get; set; }

class Thumb
    public int id { get; set; }
    public string url { get; set; }
    public int likes { get; set; }

You can then deserialize it like this (where json is a string containing the JSON data from the web service):

Dictionary<string, Artist> artists = 
          JsonConvert.DeserializeObject<Dictionary<string, Artist>>(json);

Once deserialized, you can access the data like this:

foreach (KeyValuePair<string, Artist> kvp in artists)
    Console.WriteLine("Urls for " + kvp.Key + ":");
    foreach (Thumb thumb in kvp.Value.artistthumb)

Assuming the data you showed in your question, the output would look like this:

Urls for Metallica:


share|improve this answer
Thanks that does it. –  Jameson_uk Jul 4 '13 at 20:13
great way to retrieve json, thanks –  LXG Apr 19 at 9:52

Your Answer


