JSON字符串包含空值

本文关键字:空值 包含 字符串 JSON | 更新日期: 2023-09-27 18:29:24

我有这行代码:

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

CCD_ 1和CCD_

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

JSON字符串包含空值

Message构造函数缺少两个基于用户的属性的初始化,应该如下所示:

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