本文介绍了JsonSerializationException:无法找到用于类型的构造函数...路径“数据",第1行,位置83的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是从ASP.NET Core 2.0编写的webapi返回的我的Json字符串:
This is my Json String returned from my webapi wroten by ASP.NET Core 2.0:
{
"httpStatusCode": 200,
"isSuccess": true,
"errorCode": null,
"errorDetail": null,
"data": []
}
在我的客户端中,我使用Newtonsoft.Json反序列化JsonString.
In my client I use Newtonsoft.Json to deserialize the JsonString.
JsonConvert.DeserializeObject<JsonMessage<T>>(jsonString);
在 JsonMessage
中, T
是 PageList
,在 Pagelist
中, T
是团队
.
级别太多了吗?
但是有一个例外:
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时,效果很好.
It works well when the data is not 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; }
}
推荐答案
在空构造函数中使用属性[JsonConstructor]
use Attribute [JsonConstructor] in empty constructor
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() { }
}
这篇关于JsonSerializationException:无法找到用于类型的构造函数...路径“数据",第1行,位置83的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!