使用非标准密钥的 Newtonsoft JsonConvert?

Newtonsoft JsonConvert with non-standard keys?

我正在使用 NewtonSoft 的 JsonConvert for .NET。我正在从这里取回我的 json:

http://www.last.fm/api/show/user.getRecentTracks

结果JSON如下:

"recenttracks": {
    "track": [{
        "artist": {
            "#text": "Van Halen",
            "mbid": "b665b768-0d83-4363-950c-31ed39317c15"
        },
        "name": "Dreams",
        "streamable": "0",
        "mbid": "0fc4adac-20b9-4309-8060-8ecf1360d458",
        "album": {
            "#text": "Best of Van Halen, Volume 1",
            "mbid": ""
        },
        "url": "http:\/\/www.last.fm\/music\/Van+Halen\/_\/Dreams",
        "image": [{
            "#text": "http:\/\/userserve-ak.last.fm\/serve\/34s\/98202743.jpg",
            "size": "small"
        }, {
            "#text": "http:\/\/userserve-ak.last.fm\/serve\/64s\/98202743.jpg",
            "size": "medium"
        }, {
            "#text": "http:\/\/userserve-ak.last.fm\/serve\/126\/98202743.jpg",
            "size": "large"
        }, {
            "#text": "http:\/\/userserve-ak.last.fm\/serve\/300x300\/98202743.jpg",
            "size": "extralarge"
        }],
        "@attr": {
            "nowplaying": "true"
        }
    }],
}

理想情况下,我会按如下方式访问 json 的详细信息:

MessageBox.Show(json.recenttracks.track[0].artist.ToString());

这将呼应艺术家字段的内容。但是,我只想回显 'artist[#text]' 字段的内容。我不知道如何检索这些数据,因为我不能使用 # 作为标识符。我如何获得此文本?

您可以使用 JsonProperty 属性指定应将哪个键映射到 属性。

在你的情况下是

[JsonProperty("#text")]
public string Text {get; set;}