从文本框中获取用户数据并将其写入特定的 json 名称
Taking user data from a text box and writing it to a specific json name
我能够成功地从我的文件中读取值,我只是不知道如何写入特定的名称。
我开始尝试的是
public void writeCharacter()
{
Form1 f1 = new Form1();
string homepath = Environment.GetEnvironmentVariable("homepath");
try
{
using (FileStream fs = File.Open(homepath + @"\Documents\DnD5e\charactersheet.json", FileMode.OpenOrCreate))
using (StreamWriter sw = new StreamWriter(fs))
using (JsonWriter jw = new JsonTextWriter(sw))
{
jw.Formatting = Formatting.Indented;
JsonSerializer jserialize = new JsonSerializer();
foreach (Control ctrl in f1.Controls)
{
if (ctrl.Tag == "CHANGED")
{
jserialize.Serialize(jw, ctrl.Text);
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Source + "\n\n" + e.Message);
}
}
唯一的问题是我无法弄清楚序列化程序写入的位置,也无法弄清楚如何指定要写入的名称。
我想从文本框中获取输入(当文本更改时,它们被赋予标签“已更改”),并且根据数据来自哪个文本框,我希望将其写入该特定名称。非常感谢任何帮助!!
我的代码全部托管on git
您已经有一个JsonWriter
,您不需要分配一个JsonSerializer
。 JsonWriter
是用于写出 JSON:
的低级机制
using (FileStream fs = File.Open(homepath + @"\Documents\DnD5e\charactersheet.json",
FileMode.OpenOrCreate))
using (StreamWriter sw = new StreamWriter(fs))
using (JsonWriter jw = new JsonTextWriter(sw))
{
jw.Formatting = Formatting.Indented;
foreach (Control ctrl in f1.Controls)
{
if (ctrl.Tag == "CHANGED")
{
jw.WritePropertyName(ctrl.Name);
jw.WriteValue(ctrl.Text);
}
}
}
JsonWriter
将通过您的 StreamWriter
写入文件。
我能够成功地从我的文件中读取值,我只是不知道如何写入特定的名称。
我开始尝试的是
public void writeCharacter()
{
Form1 f1 = new Form1();
string homepath = Environment.GetEnvironmentVariable("homepath");
try
{
using (FileStream fs = File.Open(homepath + @"\Documents\DnD5e\charactersheet.json", FileMode.OpenOrCreate))
using (StreamWriter sw = new StreamWriter(fs))
using (JsonWriter jw = new JsonTextWriter(sw))
{
jw.Formatting = Formatting.Indented;
JsonSerializer jserialize = new JsonSerializer();
foreach (Control ctrl in f1.Controls)
{
if (ctrl.Tag == "CHANGED")
{
jserialize.Serialize(jw, ctrl.Text);
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Source + "\n\n" + e.Message);
}
}
唯一的问题是我无法弄清楚序列化程序写入的位置,也无法弄清楚如何指定要写入的名称。
我想从文本框中获取输入(当文本更改时,它们被赋予标签“已更改”),并且根据数据来自哪个文本框,我希望将其写入该特定名称。非常感谢任何帮助!!
我的代码全部托管on git
您已经有一个JsonWriter
,您不需要分配一个JsonSerializer
。 JsonWriter
是用于写出 JSON:
using (FileStream fs = File.Open(homepath + @"\Documents\DnD5e\charactersheet.json",
FileMode.OpenOrCreate))
using (StreamWriter sw = new StreamWriter(fs))
using (JsonWriter jw = new JsonTextWriter(sw))
{
jw.Formatting = Formatting.Indented;
foreach (Control ctrl in f1.Controls)
{
if (ctrl.Tag == "CHANGED")
{
jw.WritePropertyName(ctrl.Name);
jw.WriteValue(ctrl.Text);
}
}
}
JsonWriter
将通过您的 StreamWriter
写入文件。