json反序列化错误

时间:2013-04-06 12:37:56

标签: c# .net json serialization deserialization

我在尝试将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属性中包含User对象的Band时,会弹出此错误。

我知道哪里出错了?

谢谢:)

1 个答案:

答案 0 :(得分:1)

很难说,几年前我曾经使用过jonson,但是

  1. 在jonson你有属性“密码”但在对象中你有MapField(“密码”) - 小写

  2. 在AbstractEntity类中是否为“Id”属性?