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{ }(null値)
コードの何が問題なのかわかりません。助けてください。

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 に答える