我正在使用c#中的last.fm api玩。我以前从未使用过JSON,所以这对我来说有点新。
我想解析的JSON看起来像
"{
"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"
}
},
我的代码供应JSON看起来像
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;
}
我的代码对专辑的认可似乎最初可以正常工作。但是,当我查看返回的列表时,对于每个专辑对象,我的图像列表为空,等级变量为null。
我编写了代码,该代码将导航到JSON中的专辑数组,而只是解析每张专辑,但看起来不太好。我不认为我已经为图像和topalbum课程正确编写了jsonpropertys。
这是我的对象供参考。
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; }
}
使用此站点,您的模型应该像这样
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; }
}
您现在可以作为
进行挑选var result = JsonConvert.DeserializeObject<RootObject>(json);