Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当有特殊名称时,如何通过JSON.net解析JSON数据?_C#_Json.net_Deserialization - Fatal编程技术网

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

C# 当有特殊名称时,如何通过JSON.net解析JSON数据?,c#,json.net,deserialization,C#,Json.net,Deserialization,但是,DeserializeObject方法无法将字符串反序列化到包含文本字段而不是#text字段的对象中。 下面是我对反序列化对象目标的类定义 { "artisttracks": { "track": [ { "artist": { "#text": "Flying Lotus", "mbid": "fc7376fe-1a6f-4414-b4a7-83f50ed59c92" }, "name": "Arkestry", "streamable": "0", "mbid": "5f2c6783-

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

{
"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"
}

我这样做是不正确的还是有办法获取文本?

因为您的JSON数据名是
#text
,默认JSON Convert无法与
公共字符串文本{get;set;}
属性绑定

一种简单的方法是,您可以尝试在
公共字符串文本{get;set;}
属性上添加
JsonProperty
属性

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; }
        }
 }



您的json数据在您的问题中不正确。数据不完整,因为它确实很长,json响应中有一个attr部分。必须澄清,反序列化方法工作正常,但在反序列化之后,rootresult中的公共字符串文本字段都是空的确定!!我写了一个答案,你可以试试@michael16574
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; }
        }
 }
 [JsonProperty("#text")]
 public string text { get; set; }