2010-04-08 68 views
0

我有這個類別:Block Cascade Json Serealize?

public class User 
{ 
    public string id{ get; set; } 
    public string name{ get; set; } 
    public string password { get; set; } 
    public string email { get; set; } 
    public bool is_broker { get; set; } 
    public string branch_id { get; set; } 
    public string created_at{get; set;} 
    public string updated_at{get; set;} 
    public UserGroup UserGroup {get;set;} 
    public UserAddress UserAddress { get; set; } 
    public List<UserContact> UserContact {get; set;} 

    public User() 
    { 
     UserGroup = new UserGroup(); 
     UserAddress = new UserAddress(); 
     UserContact = new List<UserContact>(); 
    } 
} 

我喜歡Serealize只有屬性,如何阻止用戶組,UserAdress的serealization,ASN UserContact ???

這是我Serealization功能:

public static string Serealize<T>(T obj) 
    { 
     System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType()); 
     MemoryStream ms = new MemoryStream(); 
     serializer.WriteObject(ms, obj); 
     return Encoding.UTF8.GetString(ms.ToArray(), 0,(int)ms.Length); 
    } 

回答

1

由於您使用的是DataContractJsonSerializer你會想要做這樣的事情:

[DataContract] 
public class User 
{ 
    [DataMember] public string id{ get; set; } 
    [DataMember] public string name{ get; set; } 
    [DataMember] public string password { get; set; } 
    [DataMember] public string email { get; set; } 
    [DataMember] public bool is_broker { get; set; } 
    [DataMember] public string branch_id { get; set; } 
    [DataMember] public string created_at{get; set;} 
    [DataMember] public string updated_at{get; set;} 
    public UserGroup UserGroup {get;set;} 
    public UserAddress UserAddress { get; set; } 
    public List<UserContact> UserContact {get; set;} 

    public User() 
    { 
     UserGroup = new UserGroup(); 
     UserAddress = new UserAddress(); 
     UserContact = new List<UserContact>(); 
    } 
} 
+0

加我serealizatoin功能! – JoeLoco 2010-04-08 16:40:44

+0

謝謝你的男人!它的工作,但我有更多的問題,我必須設置[DataMember] Deserealize,而不是[DataMember]設置爲Serealize! 任何詭計都可以做到這一點??? – JoeLoco 2010-04-08 16:51:42