I'm playing around with the Last.FM API in C#. I haven't used Json much before, so this is a bit new to me.
The json I'm trying to parse looks like
"{
\"topalbums\": {
\"album\": [
{
\"name\": \"California (Deluxe Edition)\",
\"playcount\": \"89\",
\"mbid\": \"\",
\"url\": \"https: \/\/www.last.fm\/music\/blink-182\/California+(Deluxe+Edition)\",
\"artist\": {
\"name\": \"blink-182\",
\"mbid\": \"0743b15a-3c32-48c8-ad58-cb325350befa\",
\"url\": \"https: \/\/www.last.fm\/music\/blink-182\"
},
\"image\": [
{
\"#text\": \"https: \/\/lastfm-img2.akamaized.net\/i\/u\/34s\/ccd85fcfa4370e1df83a67c7fa79096c.png\",
\"size\": \"small\"
},
{
\"#text\": \"https: \/\/lastfm-img2.akamaized.net\/i\/u\/64s\/ccd85fcfa4370e1df83a67c7fa79096c.png\",
\"size\": \"medium\"
},
{
\"#text\": \"https: \/\/lastfm-img2.akamaized.net\/i\/u\/174s\/ccd85fcfa4370e1df83a67c7fa79096c.png\",
\"size\": \"large\"
},
{
\"#text\": \"https: \/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/ccd85fcfa4370e1df83a67c7fa79096c.png\",
\"size\": \"extralarge\"
}
],
\"@attr\": {
\"rank\": \"1\"
}
},
My code to deserialize the json looks like
public List<TopAlbum> GetTopAlbumsDeserialized(int limit)
{
List<TopAlbum> topAlbumList = new List<TopAlbum>();
var request = (HttpWebRequest)WebRequest.Create(
webServiceURL + "/2.0/?method=user.gettopalbums&user=" + user + "&api_key=" + key + "&format=json");
if (limit > 0)
{
request = (HttpWebRequest)WebRequest.Create(
webServiceURL + "/2.0/?method=user.gettopalbums&user=" + user + "&limit=" + limit
+ "&api_key=" + key + "&format=json");
}
request.ContentType = "application/json; charset=utf-8";
request.Method = WebRequestMethods.Http.Get;
request.Accept = "application/json";
var response = (HttpWebResponse)request.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
string responseString = sr.ReadToEnd();
sr.Close();
JContainer jContainer = JObject.Parse(responseString);
JArray topAlbumsArray = JArray.FromObject(jContainer.First().First().First().First());
JObject.FromObject(jContainer.First().First().First().First()["image"]);
topAlbumList = JsonConvert.DeserializeObject<List<TopAlbum>>(topAlbumsArray.ToString());
}
return topAlbumList;
}
My code to deserialize the albums seemed to work mostly ok at first. But, when I look at the List that gets returned, for each Album object, my images list is empty and rank variable is null.
I've written code that will navigate to the album array in the json, and just parse each album, but it doesn't look as nice. I don't think I've written the JsonPropertys correctly for the Image and TopAlbum classes though.
Here's my objects for reference.
public class Album
{
public string name { get; set; }
public string playcount { get; set; }
public string mbid { get; set; }
public string url { get; set; }
public Artist artist { get; set; }
public List<Image> images { get; set; }
}
public class TopAlbum : Album
{
public string rank { get; set; }
}
public class Image
{
[JsonProperty("#text")]
public string text {get; set;}
public string size { get; set; }
}
public class Artist
{
public string name { get; set; }
public string mbid { get; set; }
public string url { get; set; }
}
Using this site, your model should be like this
public class Artist
{
public string name { get; set; }
public string mbid { get; set; }
public string url { get; set; }
}
public class Image
{
[JsonProperty("#text")]
public string text { get; set; }
public string size { get; set; }
}
public class Attr
{
public string rank { get; set; }
}
public class Album
{
public string name { get; set; }
public string playcount { get; set; }
public string mbid { get; set; }
public string url { get; set; }
public Artist artist { get; set; }
public List<Image> image { get; set; }
[JsonProperty("@attr")]
public Attr attr { get; set; }
}
public class Attr2
{
public string user { get; set; }
public string page { get; set; }
public string perPage { get; set; }
public string totalPages { get; set; }
public string total { get; set; }
}
public class Topalbums
{
public List<Album> album { get; set; }
[JsonProperty("@attr")]
public Attr2 attr { get; set; }
}
public class RootObject
{
public Topalbums topalbums { get; set; }
}
You can deserialize now as
var result = JsonConvert.DeserializeObject<RootObject>(json);