如何将私有字段添加到 json
How to add private fields to json
我有一个带有私有字段的 class,我希望它们在 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 变量中包含字典,该字典的值是带有其私有字段的用户对象。
(此外,如果您可以编写一个函数,该函数将在 WriteToFile class 中,而不是在其他 class 中)
谢谢!
使用JsonProperty
声明您的私有属性
public class User
{
[JsonProperty]
private string userEmail = "@abc";
[JsonProperty]
private string userPassword = "def";
public bool isconnected = true;
}
我有一个带有私有字段的 class,我希望它们在 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 变量中包含字典,该字典的值是带有其私有字段的用户对象。 (此外,如果您可以编写一个函数,该函数将在 WriteToFile class 中,而不是在其他 class 中)
谢谢!
使用JsonProperty
public class User
{
[JsonProperty]
private string userEmail = "@abc";
[JsonProperty]
private string userPassword = "def";
public bool isconnected = true;
}