无法反序列化对象Json Xamarin

时间:2017-10-25 19:04:57

标签: c# json xamarin.forms

我在xamarin中反序列化Json时遇到了麻烦。

我遇到了这个问题:

Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type RadioApp.Model.Data. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'data', line 1, position 8.

现在,这是我正在使用的课程:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RadioApp.Model
{
    public class Data
    {
        [JsonProperty("data")]
        public DJData DjData { get; set; }
    }


    public class DJData
    {
        [JsonProperty("listeners")]
        public int Listeners { get; set; }
        [JsonProperty("song")]
        public string Song { get; set; }
        [JsonProperty("live_dj")]
        public string DJ { get; set; }
        [JsonProperty("song_history")]
        public SongHistory[] song_history { get; set; }
    }


    public class SongHistory
    {
        public int playedat { get; set; }
        public string title { get; set; }
        public Metadata metadata { get; set; }
    }

    public class Metadata
    {
        public string tit2 { get; set; }
    }



}

这是我得到的字符串:

[0:] Result: {"data":{"listeners":78,"song":"Shawn Wasabi - OTTER POP (ft. Hollis)","live_dj":"Tyeisha","mini_rooster":[],"song_history":[{"playedat":1508958065,"title":"Shawn Wasabi - OTTER POP (ft. Hollis)","metadata":{"tit2":"Shawn Wasabi - OTTER POP (ft. Hollis)"}},{"playedat":1508957837,"title":"Katy Perry Ft. Nicki Minaj - Swish Swish","metadata":{"tit2":"Katy Perry Ft. Nicki Minaj - Swish Swish"}},{"playedat":1508957710,"title":"HOUSE OF GOLDTWENTY ONE PILOTS","metadata":{"tit2":"HOUSE OF GOLDTWENTY ONE PILOTS"}},{"playedat":1508957363,"title":"K.Flay - High Enough","metadata":{"tit2":"K.Flay - High Enough"}},{"playedat":1508957177,"title":"Melanie Martinez - Dollhouse (Jai Wolf Remix)","metadata":{"tit2":"Melanie Martinez - Dollhouse (Jai Wolf Remix)"}},{"playedat":1508956744,"title":"Calvin Harris Ft. Pharrell Williams, Katy Perry & Big Sean - Feels","metadata":{"tit2":"Calvin Harris Ft. Pharrell Williams, Katy Perry & Big Sean - Feels"}},{"playedat":1508956532,"title":"Major Lazer, Travis Scott, Camila Cabello, Quavo - Know No Better (feat. Quavo)","metadata":{"tit2":"Major Lazer, Travis Scott, Camila Cabello, Quavo - Know No Better (feat. Quavo)"}},{"playedat":1508956039,"title":"Darren Styles - Us Against The World (Da Tweekaz Remix)","metadata":{"tit2":"Darren Styles - Us Against The World (Da Tweekaz Remix)"}},{"playedat":1508955843,"title":"Mullaha - For You Only","metadata":{"tit2":"Mullaha - For You Only"}},{"playedat":1508955498,"title":"The Script - Rain","metadata":{"tit2":"The Script - Rain"}}]}}

我不知道这里出了什么问题,我使用相同的代码(除了每个文件只有一个类),在多个项目中,它就像一个魅力,有人可以解释我发生了什么这里吗?

1 个答案:

答案 0 :(得分:0)

正如错误消息所示,请尝试向相关类添加默认构造函数:

 public class Data
 {
    [JsonProperty("data")]
    public DJData DjData { get; set; }

    public Data() {}
 }

同时检查https://www.sharkys.com/food/menus/建议的内容:

  

在' Android选项中'项目属性的选项卡,有一个   '接头'标签。是否已选择'链接'下拉" Sdk   仅限装配"或者是" Sdk和用户程序集"?

     

如果是后者,则跳过无参数构造函数   链接,因为没有检测到使用。因此,将其更改为" Sdk Assemblies   仅"