我在尝试将 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; }
抽象实体:
namespace Entities.helpers
{
public abstract class AbstractEntity
{
[PrimaryKey, Identity, MapField("id")]
public int Id { get; set; }
public string Serialize()
{
return JsonConvert.SerializeObject(this);
}
}
}
当我发送字符串以反序列化为包含 Frontman 属性中的用户对象的 Band 时,会弹出此错误。
知道我哪里出错了吗?
谢谢 :)