Note that there are some explanatory texts on larger screens.

plurals
  1. PODealing with fanart.tv webservice response JSON and C#
    primarykey
    data
    text
    <p>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.</p> <p>The issue here is that the element names are changing. Eg. if I search for artist thumbs for Metallica you get</p> <pre><code>{"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"},...]}} </code></pre> <p>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</p> <pre><code> var obj = _downloader.Download&lt;JObject&gt;(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 =&gt; thumb["url"].ToString())); } } } } </code></pre> <p>which works fine when there is a response but if there are no thumbs the web-service returns null and this code fails with </p> <pre><code>Unable to cast object of type 'Newtonsoft.Json.Linq.JValue' to type 'Newtonsoft.Json.Linq.JObject'. </code></pre> <p>To complicate matters slightly I am sort of limited by the application and ideally I need to use</p> <pre><code>JsonConvert.DeserializeObject&lt;TE&gt;(json); </code></pre> <p>So the question is what what is the best approach to solve both of these issues?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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