2017-08-30 78 views
0

我正在玩C#中的Last.FM API。我之前沒有用過Json,所以這對我來說有點新鮮。反序列化具有無效變量名稱的嵌套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; 
     } 

我的代碼反序列化的專輯一開始就有些工作大多正常。但是,當我查看返回的List時,對於每個Album對象,我的圖像列表是空的,並且rank變量爲空。

我編寫的代碼將導航到json中的專輯數組,只是解析每個專輯,但它看起來不太好。儘管如此,我認爲我沒有爲Image和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; } 
    } 

回答

1

使用this site,你的模型應該是這樣的

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); 
+0

謝謝! 絕對要將該網站加入書籤。 –