如何将私有字段添加到json

问题描述 投票:2回答:1

我有一个私有字段的类,我希望它们在JSON字符串中,稍后我将写入文本文件。

like so

class User
{
        private string userEmail;
        private string userPassword;
        public bool isconnected;
}

class WriteToFile
{
   public Dictionary<string,User> write(){
           if (File.Exists(path))
           {
                    File.WriteAllText(path, string.Empty);
                    string json = JsonConvert.SerializeObject(dictionary);
                    File.WriteAllText(path, json);
           }
   }
}

我想在json变量中将包含他的值为User对象及其PRIVATE字段的字典。 (另外,如果你可以编写一个将在WriteToFile类中的函数,而不是在其他类中)

谢谢!

c# json dictionary private
1个回答
4
投票

JsonProperty声明你的私人财产

public class User
{
    [JsonProperty]
    private string userEmail = "@abc";
    [JsonProperty]
    private string userPassword = "def";
    public bool isconnected = true;
}
© www.soinside.com 2019 - 2024. All rights reserved.