2016-01-07 118 views
0

我有這行代碼:JSON字符串包含空值

Message m = new Message(ciphertext, cipherKey, DateTime.Now, currentUser, serverUser); 

currentUserserverUser如下

serverUser = new User("Server", "svr", "Server", serverPublicKey); 
currentUser = new User("Charlie", "c", "Charlie", publicKey); 

m被串行化

string jsonMsg = JsonConvert.SerializeObject(m); 

但已被初始化JSON字符串只顯示以下

"{\"data\":\"ylPSml/wesmAqxP4DpZc7A==\",\"encryptedKey\":\"hlEX5u9eWeQb626ea4dq8D37jKyNkGGUMtdtrpKeaZxo5LfzRbCWvB9ZY5i3aL3RsG/XTf3vhLxPHztto/UJDVqGc+tQeGqUaVbgLJhCwmppmrEvciRrv3PRd/E8pCmmD69HVSN0/Vb0546AhPaI6OJqzhUpWRC+lcE7EEKNJT4=\",\"dateTime\":\"2016-01-07T19:18:08.315557+08:00\",\"dest\":null,\"src\":null}" 

destsrc的字段都爲空。我是這樣做的錯誤還是SerializeObject函數應該這樣工作?

編輯

用戶和消息類:

public class User 
{ 
    public string displayName { get; set; } 
    public string id { get; set; } 
    public string name { get; set; } 
    public string publicKey { get; set; } 

    public User(string displayName, string id, string name, string publicKey) 
    { 
     this.displayName = displayName; 
     this.id = id; 
     this.name = name; 
     this.publicKey = publicKey; 
    } 
} 


public class Message 
{ 
    public string data { get; set; } 
    public byte[] encryptedKey { get; set; } 
    public DateTime dateTime { get; set; } 
    public User dest { get; set; } 
    public User src { get; set; } 

    public Message(string data, byte[] encryptedKey, DateTime dateTime, User dest, User src) 
    { 
     this.dateTime = dateTime; 
     this.data = data; 
     this.encryptedKey = encryptedKey; 
    } 
} 
+4

你能發佈您的*用戶*和*消息*類? – croxy

+0

理想情況下,發佈一個簡短的*完整*程序來演示問題。 –

+0

是的,你做錯了什麼,但你沒有給我們足夠的信息來說明什麼。 – Jodrell

回答

3

消息構造缺少兩個基於用戶屬性的initalisation,應該如下所示:

public Message(string data, byte[] encryptedKey, DateTime dateTime, User dest, User src) 
{ 
    this.dateTime = dateTime; 
    this.data = data; 
    this.encryptedKey = encryptedKey; 

    // Note these two lines: 
    this.dest = dest; 
    this.src = src; 
}