无法使用DataContractJsonSerializer反序列化枚举

时间:2012-10-02 04:55:00

标签: c# asp.net-mvc json serialization

REST服务返回此json字符串

"{\"UserProfile\":2,\"UserInfo\":0,\"Status\":1,\"Photo\":0,\"Video\":1,\"Comment\":0,\"ProfilePic\":0,\"Friends\":0,\"Tags\":2}"  

使用此代码反序列化:

    public static T Deserialize<T>(string json)
    {
        T obj = Activator.CreateInstance<T>();
        MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
        System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
        obj = (T)serializer.ReadObject(ms);
        ms.Close();
        ms.Dispose();
        return obj;
    }  

Security sec = Deserialize<Security>(jsonresult);  

其中:

[DataContract]
public class Security
{
    [DataMember]
    SecurityType UserProfile { get; set; }

    [DataMember]
    SecurityType UserInfo { get; set; }

    [DataMember]
    SecurityType Status { get; set; }

    [DataMember]
    SecurityType Photo { get; set; }

    [DataMember]
    SecurityType Video { get; set; }

    [DataMember]
    SecurityType Comment { get; set; }

    [DataMember]
    SecurityType ProfilePic { get; set; }

    [DataMember]
    SecurityType Friends { get; set; }

    [DataMember]
    SecurityType Tags { get; set; }
}  

和Enum:

[DataContract]
public enum SecurityType
{
    [EnumMember]
    Everyone,

    [EnumMember]
    FriendsOf,

    [EnumMember]
    Friends
}  

反序列化jsonstring后,返回Security类型,所有属性值等于Everyone(相当于零)。
并查看console.log

$.post('@Url.Action("GetSecurityProfile", "Home")', { reload: reload }, function (data) {

    console.log(data);

    $('[data-security-profile] option').eq(data.UserProfile).attr('selected', 'selected');
    $('[data-security-info] option').eq(data.UserInfo).attr('selected', 'selected');
    $('[data-security-status] option').eq(data.Status).attr('selected', 'selected');
    $('[data-security-photos] option').eq(data.Photo).attr('selected', 'selected');
    $('[data-security-videos] option').eq(data.Video).attr('selected', 'selected');
    $('[data-security-comment] option').eq(data.Comment).attr('selected', 'selected');
    $('[data-security-profilepic] option').eq(data.ProfilePic).attr('selected', 'selected');
    $('[data-security-colleagues] option').eq(data.Friends).attr('selected', 'selected');
    $('[data-security-tags] option').eq(data.Tags).attr('selected', 'selected');

});  

返回:object{ }(空值)
我无法弄清楚代码有什么问题。请帮忙。

1 个答案:

答案 0 :(得分:2)

尝试将安全类的属性设为public:

[DataContract]
public class Security
{
    [DataMember]
    public SecurityType UserProfile { get; set; }

    [DataMember]
    public SecurityType UserInfo { get; set; }

    [DataMember]
    public SecurityType Status { get; set; }

    [DataMember]
    public SecurityType Photo { get; set; }

    [DataMember]
    public SecurityType Video { get; set; }

    [DataMember]
    public SecurityType Comment { get; set; }

    [DataMember]
    public SecurityType ProfilePic { get; set; }

    [DataMember]
    public SecurityType Friends { get; set; }

    [DataMember]
    public SecurityType Tags { get; set; }
}