阅读Json的内容和显示它

阅读Json的内容和显示它

问题描述:

我反序列化JSON阅读Json的内容和显示它

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

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; 

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

+0

你得到的错误是什么? –

+2

请格式化它可读,然后我再读一遍,也许你会得到一些帮助。 – LenglBoy

+0

System.Core.dll中发生类型为'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException'的异常,但未在用户代码中处理 附加信息:绑定时无法对空引用 –

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

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