我正在制作一个基本的 WPF c# 聊天应用程序,并希望允许用户注册一个帐户。该程序将附加一个具有如下结构的 JSON 文件:
{
"user1":{
"password":"password1"
},
"user2":{
"password":"password2"
}
}
用他们的用户名替换 user1 & 2,用他们的密码替换 password1 & 2。
我试过使用括号;
//user is the name of the TextBox that the user puts their username into
//pass is the name of the password box ^
Object [user.Text] = new
{
password = new {
pass.Password
}
};
但仅此而已。
解决此问题的最简单方法是反序列化现有数据,添加新注册的用户。再次将其序列化。例如,以当前给出的示例,如果您需要一个用户。
var currentUserCollection = JsonConvert.DeserializeObject<Dictionary<string,Dictionary<string,string>>>(str);
currentUserCollection.Add("user3",new Dictionary<string,string>{["password"]="password3"});
var newJson = JsonConvert.SerializeObject(currentUserCollection);
这将产生一个输出
{
"user1": {
"password": "password1"
},
"user2": {
"password": "password2"
},
"user3": {
"password": "password3"
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句