我有一个带有私有字段的类,我希望它们在 JSON 字符串中,稍后我将写入文本文件。like soclass 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 类中,而不是其他类中)
1 回答
拉丁的传说
TA贡献1789条经验 获得超8个赞
声明你的私有属性JsonProperty
public class User
{
[JsonProperty]
private string userEmail = "@abc";
[JsonProperty]
private string userPassword = "def";
public bool isconnected = true;
}
- 1 回答
- 0 关注
- 56 浏览
添加回答
举报
0/150
提交
取消