JsonSerializationException:无法找到用于类型的构造函数...路径'数据',第1行,第83位

时间:2017-12-14 02:16:58

标签: c# list serialization asp.net-core json.net

这是我从我的webapi wroten返回的我的Json String:ASP.NET Core 2.0:

{
    "httpStatusCode": 200,
    "isSuccess": true,
    "errorCode": null,
    "errorDetail": null,
    "data": []
}

在我的客户端,我使用Newtonsoft.Json反序列化JsonString。

JsonConvert.DeserializeObject<JsonMessage<T>>(jsonString);

JsonMessageTPageList,在Pagelist TTeam

水平太高了吗?

但有一个例外:

  

JsonSerializationException:无法找到要用于的构造函数   类型Tower.Abstraction.PagedList 1[Tower.Abstraction.Model.Team]. Path 'data', line 1, position 83. JsonSerializationException: Unable to find a constructor to use for type Tower.Abstraction.PagedList 1 [Tower.Abstraction.Model.Team]。路径'数据',第1行,第83位。

Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewList(JsonReader reader, JsonArrayContract contract, out bool createdFromNonDefaultCreator)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, object existingValue, string id)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, object target)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, string id)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, object existingValue)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, bool checkAdditionalContent)
Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
Newtonsoft.Json.JsonConvert.DeserializeObject(string value, Type type, JsonSerializerSettings settings)
Newtonsoft.Json.JsonConvert.DeserializeObject<T>(string value, JsonSerializerSettings settings)

当数据不是PagedList时,它运行良好。

班级结构:

using System;
using System.Net;
using System.Runtime.Serialization;

namespace Tower.Web
{
    [Serializable]
    [DataContract]
    public class JsonMessage<T>
    {
        [DataMember]
        public HttpStatusCode HttpStatusCode { get; set; }
        [DataMember]
        public bool IsSuccess { get; set; }
        [DataMember]
        public string ErrorCode { get; set; }
        [DataMember]
        public string ErrorDetail { get; set; }
        [DataMember]
        public T Data { get; set; }
    }
}


[Serializable]
[DataContract]
public class PagedList<T> : List<T>
{
    [DataMember]
    public long TotalCount { get; set; }
    [DataMember]
    public int PageIndex { get; set; }
    [DataMember]
    public int PageSize { get; set; }
}

[DataContract]
[Serializable]
public class Team : Base
{
    [Required]
    [Key]
    [DataMember]
    public Guid TeamId { get; set; }
    [DataMember]
    [Required]
    public string TeamName { get; set; }
    [DataMember]
    public decimal TimeZone { get; set; }
    [DataMember]
    public string TeamDescription { get; set; }
    [DataMember]
    public Guid AdminUserId { get; set; }
}

1 个答案:

答案 0 :(得分:1)

在空的构造函数中使用属性[JsonConstructor]

public class QuestionModel
{
    public int Id { get; set; }
    public string Question { get; set; }
    public string CorrectAnswer { get; set; }
    public string Category { get; set; }
    public string IncorrectAnswers { get; set; }
    [JsonConstructor]
    public QuestionModel() { }
}