Json反序列化错误
本文关键字:错误 反序列化 Json | 更新日期: 2023-09-27 18:08:01
我得到这个错误,而试图反序列化JSON字符串对象:
"System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> Newtonsoft.Json.JsonSerializationException: Error converting value "{"Admin":false,"Id":1,"Password":"heslo","Nick":"jozifek"}" to type 'Entities.dbo.User'. Path 'Frontman', line 1, position 105. ---> System.ArgumentException: Could not cast or convert from System.String to Entities.dbo.User.
at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType)
at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
--- End of inner exception stack trace ---
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type)
at BussinessLogic.helpers.Serializer.Deserialize[T](String obj) in C:'Users'Ondra'documents'visual studio 2010'Projects'webServiceTestApp'BussinessLogic'helpers'Serializer.cs:line 14
at webServiceTestApp.Service1.GetSongs(String band) in C:'Users'Ondra'documents'visual studio 2010'Projects'webServiceTestApp'webServiceTestApp'Service1.asmx.cs:line 142
--- End of inner exception stack trace ---"
User.cs是这样的:
namespace Entities.dbo
{
[TableName("tbl_user")]
public class User : AbstractEntity
{
[MapField("nick")]
public string Nick { get; set; }
[MapField("password")]
public string Password { get; set; }
[MapField("admin")]
public bool Admin { get; set; }
}
}
和Band.cs是这样的:
namespace Entities.dbo
{
[TableName("tbl_band")]
public class Band : AbstractEntity
{
[MapField("name")]
public string Name { get; set; }
[MapField("frontman")]
public int FrontmanId { get; set; }
[Association(CanBeNull = false, ThisKey = "FrontmanId", OtherKey = "Id")]
public User Frontman { get; set; }
AbstractEntity:
namespace Entities.helpers
{
public abstract class AbstractEntity
{
[PrimaryKey, Identity, MapField("id")]
public int Id { get; set; }
public string Serialize()
{
return JsonConvert.SerializeObject(this);
}
}
}
和这个错误弹出当我发送字符串反序列化为乐队包含用户对象在Frontman属性..
你知道我哪里出错了吗?
谢谢:)
很难说,几年前我用过一次johnson,但是
-
在johnson中你有属性"Password",但在object中你有MapField("Password")-小写
-
是在抽象类"Id"属性?