简体   繁体   English

Newtonsoft JsonConvert与非标准密钥?

[英]Newtonsoft JsonConvert with non-standard keys?

I am using NewtonSoft's JsonConvert for .NET. 我正在使用.NET的NewtonSoft的JsonConvert。 I am retrieving my json from here: 我正在从这里检索我的json:

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

The resulting JSON is as follows: 生成的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"
        }
    }],
}

Ideally, I would access details of the json as follows: 理想情况下,我将按以下方式访问json的详细信息:

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

That would echo out the contents of the artist field. 那会呼应艺术家领域的内容。 However, I only want to echo out the contents of the 'artist[#text]' field. 但是,我只想回显'artist [#text]'字段的内容。 I dont know how to retrieve this data, as I cant use # as an identifier. 我不知道如何检索这些数据,因为我不能使用#作为标识符。 How do I get this text? 我如何获得此文字?

You can use the JsonProperty attribute to specify which key should be mapped to what property. 您可以使用JsonProperty属性来指定应将哪个键映射到哪个属性。

In your case this would be 你的情况是

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

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

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