Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在文本框中显示JSON条目,并在单击按钮时在字符串中移动?_C#_Json_Winforms - Fatal编程技术网

C# 如何在文本框中显示JSON条目,并在单击按钮时在字符串中移动?

C# 如何在文本框中显示JSON条目,并在单击按钮时在字符串中移动?,c#,json,winforms,C#,Json,Winforms,我正在VS 2017 C#中构建一个windows窗体应用程序,在这里我要求用户输入他们的主机IP地址、用户名、密码等。然后将这些信息保存到JSON文件中以分隔字符串。然后,用户可以添加任意数量的信息,然后按add添加一个包含记录数的新JSON字符串。JSON字符串如下所示: [ { "Record": 1, "IPaddress": "192.168.6.***", "Machinename": "taurus", "username": "nautitec

我正在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();
            }
private void按钮4\u单击(对象发送者,事件参数e)
{
if(MessageBox.Show(“您确定要添加吗”,“添加”,MessageBoxButtons.YesNo,MessageBoxIcon.Question)=DialogResult.Yes)
{
filePath=@“C:\Users\Sami\Desktop\Companies\Nautitech Mining Systems Pty Ltd\Code\JSON\app db.JSON”;
//更新json数据字符串
//jsonData=JsonConvert.SerializeObject(DataloggerList);
//System.IO.File.WriteAllText(文件路径,jsonData);
string text=File.ReadAllText(文件路径);
var currentList=JsonConvert.DeserializeObject(文本);
//创建新的数据记录器
数据记录器我自己=新的数据记录器
{
记录=++计数,
IPaddress=textBox2.Text,
Machinename=textBox8.Text,
username=textBox4.Text,
密码=textBox3.Text,
sourcefolder=textBox7.Text,
destfolder=textBox6.Text,
filextension=textBox5.Text,
};
if(currentList!=null&¤tList.Any())
{
var lastRecordNumner=currentList.OrderBy(q=>q.Record).Last().Record;
我自己。Record=lastRecordNumner+1;
}
其他的
{
currentList=新列表();
}
当前列表。添加(我自己);
字符串输出=Newtonsoft.Json.JsonConvert.SerializeObject(currentList,Newtonsoft.Json.Formatting.Indented);
控制台写入线(输出);
writealText(文件路径,输出);
}
其他的
{
这个。激活();
}
要求:
当用户输入信息并添加多个JSON字符串时,我想让用户能够单击向前向后最后一个第一个,在JSON文件中移动并在文本框中显示信息,这样他们就可以在不打开JS的情况下查看信息在上,(这是手动创建的)最终,用户可以使用保存按钮浏览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++;
}
...

只需将json反序列化为对象数组(为此编写dto类)并使用数组当我将json反序列化为数组时,如何通过单击“前进”和“后退”按钮来跟踪json字符串位置?在反序列化后,忘记json,使用数组(前进-数组的下一个元素,后退-上一个)这段代码是在你的调试器上测试的吗?它似乎不起作用,你能提供更多的背景吗?这只是一个逻辑示例