如何将私有字段添加到json

我有一个带有私有字段的类,我希望它们在 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 变量中包含字典,他的值是带有他的 PRIVATE 字段的用户对象。(此外,如果您可以编写一个函数,该函数将位于 WriteToFile 类中,而不是其他类中)


撒科打诨
浏览 59回答 1
1回答

拉丁的传说

声明你的私有属性JsonPropertypublic class User{&nbsp; &nbsp; [JsonProperty]&nbsp; &nbsp; private string userEmail = "@abc";&nbsp; &nbsp; [JsonProperty]&nbsp; &nbsp; private string userPassword = "def";&nbsp; &nbsp; public bool isconnected = true;}
打开App,查看更多内容
随时随地看视频慕课网APP