有特殊名称时,如何通过Json.net解析JSON数据?

时间:2018-07-06 20:09:12

标签: c# json.net deserialization

using (var client = new HttpClient())
        {
            HttpResponseMessage response = client.GetAsync(url).Result;
            if (response.IsSuccessStatusCode)
            {
                string result = await response.Content.ReadAsStringAsync();
                var rootResult = JsonConvert.DeserializeObject<LastFMResponse_ArtistTracks>(result);
                return rootResult;
            }
            else
            {
                return null;
            }
        }

上面的字符串结果返回完整的json响应,看起来像

{
"artisttracks": {
"track": [
{
"artist": {
"#text": "Flying Lotus",
"mbid": "fc7376fe-1a6f-4414-b4a7-83f50ed59c92"
},
"name": "Arkestry",
"streamable": "0",
"mbid": "5f2c6783-f876-4bbe-9577-d3498da6b0ab",
"album": {
"#text": "Cosmogramma",
"mbid": "7369257e-2346-4fe6-8810-c92c409d6671"
},
"url": "https://www.last.fm/music/Flying+Lotus/_/Arkestry",
"image": [
{
"#text": "https://lastfm-img2.akamaized.net/i/u/34s/50227cde795b4702c7b4d7ddcf0b85ff.png",
"size": "small"
},
{
"#text": "https://lastfm-img2.akamaized.net/i/u/64s/50227cde795b4702c7b4d7ddcf0b85ff.png",
"size": "medium"
},
{
"#text": "https://lastfm-img2.akamaized.net/i/u/174s/50227cde795b4702c7b4d7ddcf0b85ff.png",
"size": "large"
},
{
"#text": "https://lastfm-img2.akamaized.net/i/u/300x300/50227cde795b4702c7b4d7ddcf0b85ff.png",
"size": "extralarge"
}
],
"date": {
"uts": "1530728536",
"#text": "04 Jul 2018, 18:22"
}

但是,DeserializeObject方法无法将字符串反序列化为包含文本字段而不是#text的对象。 以下是我对DeserializeObject目标的类定义。

public class LastFMResponse_ArtistTracks
{
        public Artisttracks artisttracks { get; set; }

        public class Artisttracks
        {
            public Track[] track { get; set; }
            public Attr @attr { get; set; }
        }
        public class Attr
        {
            public string user { get; set; }
            public string artist { get; set; }
            public string page { get; set; }
            public string perPage { get; set; }
            public string totalPages { get; set; }
            public string total { get; set; }
        }
        public class Track
        {
            public Artist artist { get; set; }
            public string name { get; set; }
            public string streamable { get; set; }
            public string mbid { get; set; }
            public Album album { get; set; }
            public string url { get; set; }
            public Image[] image { get; set; }
            public Date date { get; set; }
        }
        public class Artist
        {
            public string text { get; set; }
            public string mbid { get; set; }
        }
        public class Album
        {
            public string text { get; set; }
            public string mbid { get; set; }
        }
        public class Date
        {
            public string uts { get; set; }
            public string text { get; set; }
        }
        public class Image
        {
            public string text { get; set; }
            public string size { get; set; }
        }
 }

我这样做是不正确的方式还是有获取文字的方式?

1 个答案:

答案 0 :(得分:1)

由于您的JSON数据名称为Category,因此默认JSON转换无法与#text属性绑定

一种简单的方法,您可以尝试在public string text { get; set; }属性上添加JsonProperty属性。

public string text { get; set; }

C# Demo


JsonPropertyAttribut