无法反序列化当前JSON对象xamarin.forms

无法反序列化当前JSON对象xamarin.forms,xamarin,xamarin.forms,dotnet-httpclient,Xamarin,Xamarin.forms,Dotnet Httpclient,我正在使用xamarin.forms创建一个使用MusixMatch api的应用程序。它引发以下异常:无法将当前JSON对象(例如,{“name\”:“value\”})反序列化为“System.Collections.Generic.List”类型。据我所知,我做的一切都是正确的,不知道为什么会抛出这个异常。任何帮助都将不胜感激 TrackList.cs public class TrackList { public class Track {

我正在使用xamarin.forms创建一个使用MusixMatch api的应用程序。它引发以下异常:无法将当前JSON对象(例如,{“name\”:“value\”})反序列化为“System.Collections.Generic.List”类型。据我所知,我做的一切都是正确的,不知道为什么会抛出这个异常。任何帮助都将不胜感激

TrackList.cs

public class TrackList
    {
        public class Track
        {
            public int track_id { get; set; }
            public string track_mbid { get; set; }
            public string track_isrc { get; set; }
            public string track_spotify_id { get; set; }
            public string track_soundcloud_id { get; set; }
            public string track_xboxmusic_id { get; set; }
            public string track_name { get; set; }
            public int track_rating { get; set; }
            public int track_length { get; set; }
            public int commontrack_id { get; set; }
            public int instrumental { get; set; }
        }
        public class Body
        {
            public IList<Track> track_list { get; set; }
        }

    }

我认为不能将单个对象反序列化到集合中。因此,您应该使用
TrackList.Body
而不是
List

所以你可能需要改变这一行:

var items=JsonConvert.DeserializeObject(内容)

var items=JsonConvert.DeserializeObject(内容)


然后迭代
items
中的每个项,将它们添加到
列表中

您告诉它将结果反序列化到列表中,而实际结果是具有1个属性(消息)且具有2个属性(头和体)的对象,因此请确保对象结构匹配。我发现这样的复杂结构非常方便

public class TrackListResponse
{
    public Message message { get; set; }

    public class Header
    {
        public int status_code { get; set; }
        public double execute_time { get; set; }
        public int available { get; set; }
    }

    public class PrimaryGenres
    {
        public List<object> music_genre_list { get; set; }
    }

    public class SecondaryGenres
    {
        public List<object> music_genre_list { get; set; }
    }

    public class Track
    {
        public int track_id { get; set; }
        public string track_mbid { get; set; }
        public string track_isrc { get; set; }
        public string track_spotify_id { get; set; }
        public string track_soundcloud_id { get; set; }
        public string track_xboxmusic_id { get; set; }
        public string track_name { get; set; }
        public List<object> track_name_translation_list { get; set; }
        public int track_rating { get; set; }
        public int track_length { get; set; }
        public int commontrack_id { get; set; }
        public int instrumental { get; set; }
        public int @explicit { get; set; }
        public int has_lyrics { get; set; }
        public int has_lyrics_crowd { get; set; }
        public int has_subtitles { get; set; }
        public int has_richsync { get; set; }
        public int num_favourite { get; set; }
        public int lyrics_id { get; set; }
        public int subtitle_id { get; set; }
        public int album_id { get; set; }
        public string album_name { get; set; }
        public int artist_id { get; set; }
        public string artist_mbid { get; set; }
        public string artist_name { get; set; }
        public string album_coverart_100x100 { get; set; }
        public string album_coverart_350x350 { get; set; }
        public string album_coverart_500x500 { get; set; }
        public string album_coverart_800x800 { get; set; }
        public string track_share_url { get; set; }
        public string track_edit_url { get; set; }
        public string commontrack_vanity_id { get; set; }
        public int restricted { get; set; }
        public DateTime first_release_date { get; set; }
        public DateTime updated_time { get; set; }
        public PrimaryGenres primary_genres { get; set; }
        public SecondaryGenres secondary_genres { get; set; }
    }

    public class TrackList
    {
        public Track track { get; set; }
    }

    public class Body
    {
        public List<TrackList> track_list { get; set; }
    }

    public class Message
    {
        public Header header { get; set; }
        public Body body { get; set; }
    }
}
公共类TrackListResponse
{
公共消息{get;set;}
公共类标题
{
公共int状态_代码{get;set;}
公共双执行时间{get;set;}
公共int可用{get;set;}
}
公共阶级初级流派
{
公共列表音乐类型列表{get;set;}
}
第二类公共课
{
公共列表音乐类型列表{get;set;}
}
公共类轨道
{
public int track_id{get;set;}
公共字符串磁道\u mbid{get;set;}
公共字符串轨道_isrc{get;set;}
公共字符串轨道\u spotify\u id{get;set;}
公共字符串曲目\u soundcloud\u id{get;set;}
公共字符串曲目xboxmusic\u id{get;set;}
公共字符串轨迹_name{get;set;}
公共列表轨道\名称\翻译\列表{get;set;}
公共int轨道_评级{get;set;}
公共整数轨道长度{get;set;}
public int commontrack_id{get;set;}
公共int工具{get;set;}
public int@explicit{get;set;}
public int有{get;set;}
公共int有_歌词_人群{get;set;}
public int有_副标题{get;set;}
public int具有_richsync{get;set;}
public int num_favorite{get;set;}
public int\u id{get;set;}
public int subtitle_id{get;set;}
public int相册_id{get;set;}
公共字符串相册\u名称{get;set;}
公共整数艺术家_id{get;set;}
公共字符串艺术家\u mbid{get;set;}
公共字符串艺术家_name{get;set;}
公共字符串相册_coverart_100x100{get;set;}
公共字符串相册_coverart_350x350{get;set;}
公共字符串相册\u coverart\u 500x500{get;set;}
公共字符串相册\u coverart\u 800x800{get;set;}
公共字符串跟踪\u共享\u url{get;set;}
公共字符串跟踪\u编辑\u url{get;set;}
公共字符串commontrack\u vanity\u id{get;set;}
公共int受限{get;set;}
公共日期时间首次发布日期{get;set;}
公共日期时间已更新\u时间{get;set;}
公共主要类型主要类型{get;set;}
public secondary体裁secondary_体裁{get;set;}
}
公共类跟踪列表
{
公共轨道轨道{get;set;}
}
公共阶级团体
{
公共列表轨道\u列表{get;set;}
}
公共类消息
{
公共标头{get;set;}
公共机构主体{get;set;}
}
}
然后反序列化到外部对象:

JsonConvert.DeserializeObject<TrackListResponse>(content);
JsonConvert.DeserializeObject(内容);

您是否刚刚在公共网站上发布了API密钥。。?也许不是最好的做法:)我通常从生成一个类开始,然后按照我想要的方式更改命名,忘了这一点。谢谢你。将尝试您的建议您的api密钥仍在问题历史记录中。看起来您尝试序列化的类型与JSON结构不匹配。谢谢。我已经尝试过了,异常消失了,但是现在“items”返回null,即使在调试时,我可以看到响应状态是200,并被传递到“content”,为了更好地理解,我已经更新了我的帖子,将json包括在内(谢谢:)。我已经尝试过了,但是属性在“items”中返回null,但是值被成功地传递到了“content”中。我可能在第一次使用body对象时有点错误。答案已更新-json2csharp进行救援。:)非常感谢托德:D。现在可以用了。只是属性现在没有绑定到我的xaml,但我会找到答案的。谢谢
public class TrackListResponse
{
    public Message message { get; set; }

    public class Header
    {
        public int status_code { get; set; }
        public double execute_time { get; set; }
        public int available { get; set; }
    }

    public class PrimaryGenres
    {
        public List<object> music_genre_list { get; set; }
    }

    public class SecondaryGenres
    {
        public List<object> music_genre_list { get; set; }
    }

    public class Track
    {
        public int track_id { get; set; }
        public string track_mbid { get; set; }
        public string track_isrc { get; set; }
        public string track_spotify_id { get; set; }
        public string track_soundcloud_id { get; set; }
        public string track_xboxmusic_id { get; set; }
        public string track_name { get; set; }
        public List<object> track_name_translation_list { get; set; }
        public int track_rating { get; set; }
        public int track_length { get; set; }
        public int commontrack_id { get; set; }
        public int instrumental { get; set; }
        public int @explicit { get; set; }
        public int has_lyrics { get; set; }
        public int has_lyrics_crowd { get; set; }
        public int has_subtitles { get; set; }
        public int has_richsync { get; set; }
        public int num_favourite { get; set; }
        public int lyrics_id { get; set; }
        public int subtitle_id { get; set; }
        public int album_id { get; set; }
        public string album_name { get; set; }
        public int artist_id { get; set; }
        public string artist_mbid { get; set; }
        public string artist_name { get; set; }
        public string album_coverart_100x100 { get; set; }
        public string album_coverart_350x350 { get; set; }
        public string album_coverart_500x500 { get; set; }
        public string album_coverart_800x800 { get; set; }
        public string track_share_url { get; set; }
        public string track_edit_url { get; set; }
        public string commontrack_vanity_id { get; set; }
        public int restricted { get; set; }
        public DateTime first_release_date { get; set; }
        public DateTime updated_time { get; set; }
        public PrimaryGenres primary_genres { get; set; }
        public SecondaryGenres secondary_genres { get; set; }
    }

    public class TrackList
    {
        public Track track { get; set; }
    }

    public class Body
    {
        public List<TrackList> track_list { get; set; }
    }

    public class Message
    {
        public Header header { get; set; }
        public Body body { get; set; }
    }
}
JsonConvert.DeserializeObject<TrackListResponse>(content);