我正在 VS 2017 C# 中构建一个 windows 窗体应用程序,我要求用户输入他们的主机 IP 地址、用户名、密码等。然后将这些信息保存到一个 JSON 文件中以单独的字符串单击此处查看应用程序界面。然后,用户可以添加任意数量的信息,然后按ADD添加带有记录数的新 JSON 字符串。JSON 字符串如下所示:
[
{
"Record": 1,
"IPaddress": "192.168.6.***",
"Machinename": "taurus",
"username": "nautitech",
"password": "nautitech",
"sourcefolder": "...../..../..../.....",
"destfolder": "...../..../..../.....",
"filextension": "txt",
"removedownloaded": 0
},
{
"Record": 2,
"IPaddress": "192.168.255.***",
"Machinename": "taurus",
"username": "root",
"password": "root",
"sourcefolder": "...../..../..../.....",
"destfolder": "...../..../..../.....",
"filextension": "json",
"removedownloaded": 0
},
{
"Record": 3,
"IPaddress": "192.168.10.***",
"Machinename": "taurus",
"username": "root",
"password": "root",
"sourcefolder": "...../..../..../.....",
"destfolder": "...../..../..../.....",
"filextension": "db",
"removedownloaded": 0
}
]
编写这些 JSON 字符串的代码如下:
private void button4_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you want to Add", "ADD", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
filePath = @"C:\Users\Sami\Desktop\Companies\Nautitech Mining Systems Pty Ltd\Code\JSON\app-db.json";
// Update json data string
//jsonData = JsonConvert.SerializeObject(DataloggerList);
//System.IO.File.WriteAllText(filePath, jsonData);
string text = File.ReadAllText(filePath);
var currentList = JsonConvert.DeserializeObject<List<Datalogger>>(text);
//Create new Datalogger
Datalogger myself = new Datalogger
{
Record = ++count,
IPaddress = textBox2.Text,
Machinename = textBox8.Text,
username = textBox4.Text,
password = textBox3.Text,
sourcefolder = textBox7.Text,
destfolder = textBox6.Text,
filextension = textBox5.Text,
};
if (currentList != null && currentList.Any())
{
var lastRecordNumner = currentList.OrderBy(q => q.Record).Last().Record;
myself.Record = lastRecordNumner + 1;
}
else
{
currentList = new List<Datalogger>();
}
currentList.Add(myself);
string output = Newtonsoft.Json.JsonConvert.SerializeObject(currentList, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine(output);
File.WriteAllText(filePath, output);
}
else
{
this.Activate();
}
要求:当用户输入信息并添加多个 JSON 字符串时,我想让用户能够点击Forward、Backward、Last和First来在 JSON 文件中移动并在文本框中显示信息,以便他们可以看到无需打开 JSON 的信息,就像这样(这是手动创建的)最终用户可以浏览 JSON 文件并使用“保存”按钮更新信息。
您需要再次读取您的 json 并将其反序列化并在循环中输入像 i 这样的后台计数器。根据此计数器,您需要从反序列化列表中获取您的信息。例如像这样。
#region Global Scope
public int counter;
#endregion
....
var map = JObject.Parse(jsonTextFromFile);
var serversList = (List) map;
var server;
int i = 0;
foreach (var elem in serverList)
{
if (i == counter)
server = elem;
i++;
}
...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句