c# - 如何将私有字段添加到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 类中的函数,而不是在其他类中)
谢谢!
解决方案
声明您的私有财产JsonProperty
public class User
{
[JsonProperty]
private string userEmail = "@abc";
[JsonProperty]
private string userPassword = "def";
public bool isconnected = true;
}
推荐阅读
- angular - Angular routerLink 只触发一次 ngOnInit
- python - python - 如何将具有多个聚合字段和多个索引字段的pandas数据框旋转到python中的sumIfs?
- google-apps-script - 脚本仅向列表中的最后一个人发送电子邮件
- node.js - 将角度应用程序部署到heroku时找不到模块
- python - 测试 Django 中 ForeignKey 的重复条目“1”
- applescript - 在工作流程中将项目从 Automator 传递到 AppleScript
- apache-kafka - kafka流中的消息加密
- bootstrap-4 - 使用 Boostrap 嵌入 Instagram 视频
- java - 如何在不丢失请求的情况下将 REST 请求排队/在 REST 端点上暂停处理一段时间?
- php - 将 TWIG 转换为 PHP 语法