简体   繁体   English

读取 Json 内容并显示它

[英]Reading Json Content and Displaying it

I am deserializing JSON我正在反序列化 JSON

{ "topalbums":{ "album":[ { "name":"Slumdog Millionaire", "playcount":1442872, "url":" https://www.last.fm/music/AR+Rahman/Slumdog+Millionaire ", "artist":{ "name":"AR Rahman", "mbid":"e0bba708-bdd3-478d-84ea-c706413bedab", "url":" https://www.last.fm/music/AR+Rahman " }, "image":[ { "#text":" https://lastfm-img2.akamaized.net/i/u/34s/14c3f3bc7834415db7765563177e4bf6.png ", "size":"small" }, { "#text":" https://lastfm-img2.akamaized.net/i/u/64s/14c3f3bc7834415db7765563177e4bf6.png ", "size":"medium" }, { "#text":" https://lastfm-img2.akamaized.net/i/u/174s/14c3f3bc7834415db7765563177e4bf6.png ", "size":"large" }, { "#text":" https://lastfm-img2.akamaized.net/i/u/300x300/14c3f3bc7834415db7765563177e4bf6.png ", "size":"extralarge" } ] } ] } } { "topalbums":{ "album":[ { "name":"Slumdog Millionaire", "playcount":1442872, "url":" https://www.last.fm/music/AR+Rahman/Slumdog+百万富翁", "艺术家":{ "name":"AR Rahman", "mbid":"e0bba708-bdd3-478d-84ea-c706413bedab", "url":" https://www.last.fm/music/ AR+Rahman " }, "image":[ { "#text":" https://lastfm-img2.akamaized.net/i/u/34s/14c3f3bc7834415db7765563177e4bf6.png ", "size":"small" }, { "#text":" https://lastfm-img2.akamaized.net/i/u/64s/14c3f3bc7834415db7765563177e4bf6.png ", "size":"medium" }, { "#text":" https:// lastfm-img2.akamaized.net/i/u/174s/14c3f3bc7834415db7765563177e4bf6.png ", "size":"large" }, { "#text":" https://lastfm-img2.akamaized.net/i/u /300x300/14c3f3bc7834415db7765563177e4bf6.png ", "size":"extralarge" } ] } ] } }

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;

I'm unable to figure out how to read them properly.我无法弄清楚如何正确阅读它们。

The node for albums is an array so you must access a position, 0 gets the first album. albums的节点是一个数组,因此您必须访问一个位置, 0获取第一个专辑。 In your example there's only one album anyway...在您的示例中,无论如何只有一张专辑......

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM