【问题标题】:Reading Json Content and Displaying it读取 Json 内容并显示
【发布时间】:2017-10-17 13:20:44
【问题描述】:

我正在反序列化 JSON

{ “顶级专辑”:{ “专辑”:[ { "name":"贫民窟的百万富翁", “播放次数”:1442872, "url":"https://www.last.fm/music/A.R.+Rahman/Slumdog+Millionaire", “艺术家”:{ "name":"A.R. 拉赫曼", “mbid”:“e0bba708-bdd3-478d-84ea-c706413bedab”, "网址":"https://www.last.fm/music/A.R.+Rahman" }, “图片”:[ { "#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;

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

【问题讨论】:

  • 您遇到的错误是什么?
  • 请把它格式化成可读的,然后我再读一遍,也许你会得到一些帮助。
  • System.Core.dll 中出现“Microsoft.CSharp.RuntimeBinder.RuntimeBinderException”类型的异常,但未在用户代码中处理其他信息:无法对空引用执行运行时绑定
  • 绑定时出现异常

标签: c# asp.net json webforms


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多