1

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; }
}  

和枚举:

[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{ }空值)
我无法弄清楚代码有什么问题。请帮忙。

4

1 回答 1

2

尝试公开Security类的属性:

[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; }
}  
于 2012-10-02T05:02:36.110 回答