【问题标题】:JsonSerializationException: Unable to find a constructor to use for type ... Path 'data', line 1, position 83JsonSerializationException:找不到用于类型的构造函数...路径“数据”,第 1 行,位置 83
【发布时间】:2018-05-28 01:04:39
【问题描述】:

这是我的由 ASP.NET Core 2.0 编写的 webapi 返回的 Json 字符串:

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

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

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

JsonMessageTPageList,在PagelistTTeam

是不是太多关卡了?

但有一个例外:

JsonSerializationException: 找不到要用于的构造函数 键入 Tower.Abstraction.PagedList1[Tower.Abstraction.Model.Team]. Path 'data', line 1, position 83. JsonSerializationException: Unable to find a constructor to use for type Tower.Abstraction.PagedList1[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; }
}

【问题讨论】:

  • 向我们展示您的代码以及类结构。
  • 您是否有机会与重现问题的 JSON 和 c# 类型共享 minimal reproducible example?异常意味着 Json.NET 无法选择构造函数来构造您的类型之一;有关其他示例,请参见 herehere。但是我们需要查看您的实际类型才能知道确切的问题。
  • 嗨@RawitasKrungkaew,我添加了我的类结构。
  • 我无法在我当前的设置中重现这一点。你能分享一下1)T的类型传递给JsonConvert.DeserializeObject&lt;JsonMessage&lt;T&gt;&gt;(jsonString);吗? 2) 异常的完整ToString() 输出,包括回溯、异常类型、消息和内部异常?

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


【解决方案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() { }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    • 2013-04-22
    • 1970-01-01
    相关资源
    最近更新 更多