locked
cannot deserialize the current json object (eg {"name":"value"}) on spotify api

    Question

  •  public class TrackSpotify
        {
            public class ExternalUrls
    {
        public string spotify { get; set; }
    }
    
    public class Image
    {
        public int height { get; set; }
        public string url { get; set; }
        public int width { get; set; }
    }
    
    public class Item
    {
        public string album_type { get; set; }
        public List<string> available_markets { get; set; }
        public ExternalUrls external_urls { get; set; }
        public string href { get; set; }
        public string id { get; set; }
        public List<Image> images { get; set; }
        public string name { get; set; }
        public string type { get; set; }
        public string uri { get; set; }
    }
    
    public class Albums
    {
        public string href { get; set; }
        public List<Item> items { get; set; }
        public int limit { get; set; }
        public string next { get; set; }
        public int offset { get; set; }
        public object previous { get; set; }
        public int total { get; set; }
    }
    
    public class RootObject
    {
        public Albums albums { get; set; }
    }
    {
      "albums" : {
        "href" : "https://api.spotify.com/v1/browse/new-releases?offset=0&limit=20",
        "items" : [ {
          "album_type" : "single",
          "available_markets" : [ "AD", "AR", "AT", "AU", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "ES", "FR", "GR", "GT", "HK", "HN", "HU", "IE", "IS", "IT", "LI", "MC", "MT", "MX", "MY", "NI", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "RO", "SG", "SI", "SK", "SV", "TR", "TW", "UY" ],
          "external_urls" : {
            "spotify" : "https://open.spotify.com/album/60mvULtYiNSRmpVvoa3RE4"
          },
          "href" : "https://api.spotify.com/v1/albums/60mvULtYiNSRmpVvoa3RE4",
          "id" : "60mvULtYiNSRmpVvoa3RE4",
          "images" : [ {
            "height" : 640,
            "url" : "https://i.scdn.co/image/8642802d13a53541e313781c34521a0d33099aac",
            "width" : 640
          }, {
            "height" : 300,
            "url" : "https://i.scdn.co/image/631ee4d5160303af86751587457b1b00957e0519",
            "width" : 300
          }, {
            "height" : 64,
            "url" : "https://i.scdn.co/image/d7b7140400d985d1294d7b044da1b5b4bfc0ae69",
            "width" : 64
          } ],
          "name" : "We Are One (Ole Ola) [The Official 2014 FIFA World Cup Song]",
          "type" : "album",
          "uri" : "spotify:album:60mvULtYiNSRmpVvoa3RE4"
        },

    Code:

    public async System.Threading.Tasks.Task<string> QueryTrack()
            {
                HttpClient client = new HttpClient();
                string url = "https://api.spotify.com/v1/browse/new-releases?client_id=" + API_key + "&country=US&offset=0&limit=20&access_token=....h";
                HttpResponseMessage response = await client.GetAsync(url);
                HttpResponseMessage v = new HttpResponseMessage();
                return await response.Content.ReadAsStringAsync();
            }
    ....
                var query = await queryTrack.QueryTrack();
                var arrays = JsonConvert.DeserializeObject < List < ViewModel.TrackSpotify >> (query);//Errors
                spotifyItems.ItemsSource = arrays;
    ....

    Error:

    An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.DLL
    but was not handled in user code Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"})
    into type 'System.Collections.Generic.List`1[Music.ViewModel.TrackSpotify]'
    because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3])
    or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type
    like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.



    Tuesday, May 12, 2015 4:06 AM

All replies

  • Hi Fast,

               JSON.NET recommends that you post your questions concerning that API to StackOverflow with the tag "JSON.NET".

    http://stackoverflow.com/questions/tagged/json.net



    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, May 12, 2015 3:20 PM
    Moderator