阅读Json内容并显示它

时间:2017-10-17 13:20:45

标签: c# asp.net json webforms

我正在反序列化JSON

  

{   " topalbums":{   "专辑":[   {   "姓名":"贫民窟的百万富翁",   " playcount":1442872,   " URL":" https://www.last.fm/music/A.R.+Rahman/Slumdog+Millionaire&#34 ;,   "艺术家":{   "名称":" A.R。拉赫曼&#34 ;,   " MBID":" e0bba708-bdd3-478d-84ea-c706413bedab&#34 ;,   " URL":" https://www.last.fm/music/A.R.+Rahman"   },   "图像":[   {   "#文本":" https://lastfm-img2.akamaized.net/i/u/34s/14c3f3bc7834415db7765563177e4bf6.png&#34 ;,   "大小":"小"   },   {   "#文本":" https://lastfm-img2.akamaized.net/i/u/64s/14c3f3bc7834415db7765563177e4bf6.png&#34 ;,   "大小":"介质"   },   {   "#文本":" https://lastfm-img2.akamaized.net/i/u/174s/14c3f3bc7834415db7765563177e4bf6.png&#34 ;,   "大小":"大"   },   {   "#文本":" https://lastfm-img2.akamaized.net/i/u/300x300/14c3f3bc7834415db7765563177e4bf6.png&#34 ;,   "大小":"海"   }   ]   }   ]   }   }

string artist = txtMusic.Text;
var requestUrl = "http://ws.audioscrobbler.com/2.0/?method=artist.gettopalbums&artist=" + artist + "&api_key=&format=json";

var client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome / 58.0.3029.110 Safari / 537.36");

var response = client.DownloadString(requestUrl);

Response.Write(response);
dynamic stuff = JObject.Parse(response);
string name = stuff.album.name;
lblInfo.InnerText = name;

我无法弄清楚如何正确阅读它们。

1 个答案:

答案 0 :(得分:1)

albums的节点是一个数组,因此您必须访问一个位置,0才能获得第一张专辑。在你的例子中,无论如何只有一张专辑......

string name = stuff.topalbums.album[0].name.ToString();