Reputation: 491
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": []
}
In my client I use Newtonsoft.Json to deserialize the JsonString.
JsonConvert.DeserializeObject<JsonMessage<T>>(jsonString);
In JsonMessage
the T
is PageList
, In Pagelist
the T
is Team
.
Is it too many level?
But there is a exception that:
JsonSerializationException: Unable to find a constructor to use for type 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]. Path 'data', line 1, position 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)
It works well when the data is not PagedList.
The class structure:
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; }
}
Upvotes: 2
Views: 7864
Reputation: 71
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() { }
}
Upvotes: 2