1

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 プロパティに User オブジェクトを含む Band として逆シリアル化する文字列を送信すると発生します。

私がどこで間違ったのか分かりますか?

ありがとう :)

4

1 に答える 1

1

言うのは難しいです、私は数年前に一度ジョンソンを使用しましたが、

  1. ジョンソンにはプロパティ "Password" がありますが、オブジェクトには MapField("password") があります - 小文字で

  2. AbstractEntityクラスの「Id」プロパティにありますか?

于 2013-04-06T13:04:40.240 に答える