How to iterate through a JSON file Backward and Forward and stop when it reaches the end?

  • Thread starter Thread starter samiarja
  • Start date Start date
S

samiarja

Guest
I am attempting to create a windows form application that takes the user input from a textboxes and store them in a .JSON file the application interface is shown below:

1509576.png


and this is how the data is being saved

[
{
"Record": 1,
"IPaddress": "192.168.6.55",
"Machinename": "taurus",
"username": "root",
"password": "8888",
"sourcefolder": "/home/root/data/completed",
"destfolder": "C:\\temp\\Data2",
"filextension": ".db",
"removedownloaded": 1,
"removecsv": 0,
"removedb": 0,
"eagleIOHost": "sftp.eagle.io",
"eagleIOUsername": "raven-robin-hope",
"eagleIOpassword": "nautitech250",
"eagleIOdirectory": "/usr/share/tomcat8"
},
{
"Record": 2,
"IPaddress": "192.168.6.60",
"Machinename": "net",
"username": "root",
"password": "8888",
"sourcefolder": "/home/root/data/completed",
"destfolder": "C:\\temp\\Data2",
"filextension": ".db",
"removedownloaded": 0,
"removecsv": 1,
"removedb": 1,
"eagleIOHost": "sftp.eagle.io",
"eagleIOUsername": "raven-robin-hope",
"eagleIOpassword": "nautitech250",
"eagleIOdirectory": "/usr/share/tomcat8"
}
]


In the application UI there is a button to display the values in the textboxes and two buttons to move through the data forward and backward, however when it reaches the end of the data array it shows an error.


I am unable to make a condition that knows that this is the end of the array and show a notification that tells the user that there is no more data to display.

The code to move through the data backward is below:

private void Button7_Click(object sender, EventArgs e)
{

string text = File.ReadAllText(filePath);
var currentList = JsonConvert.DeserializeObject<List<Datalogger>>(text);

if (currentList != null && currentList.Any())
{
var item1 = currentList.Where(x => x.Record.ToString() == textBox1.Text
&& x.IPaddress == textBox2.Text
&& x.Machinename == textBox8.Text
&& x.username == textBox4.Text
&& x.password == textBox3.Text
&& x.sourcefolder == textBox7.Text
&& x.destfolder == textBox6.Text
&& x.eagleIOUsername == textBox9.Text
&& x.eagleIOpassword == textBox11.Text
&& x.eagleIOHost == textBox12.Text
&& x.eagleIOdirectory == textBox13.Text
&& x.filextension == textBox5.Text).FirstOrDefault();

var item = currentList.Where(m => m.Record == item1.Record - 1).FirstOrDefault();
textBox1.Text = item.Record.ToString();
textBox2.Text = item.IPaddress;
textBox8.Text = item.Machinename;
textBox4.Text = item.username;
textBox3.Text = item.password;
textBox7.Text = item.sourcefolder;
textBox6.Text = item.destfolder;
textBox5.Text = item.filextension;
textBox9.Text = item.eagleIOUsername;
textBox11.Text = item.eagleIOpassword;
textBox12.Text = item.eagleIOHost;
textBox13.Text = item.eagleIOdirectory;


}
else
{
currentList = new List<Datalogger>();
}
}
and The code to move through the data forward is below:


private void Button6_Click(object sender, EventArgs e)
{

string text = File.ReadAllText(filePath);
var currentList = JsonConvert.DeserializeObject<List<Datalogger>>(text);
if (currentList != null && currentList.Any())
{
var item1 = currentList.Where(x => x.Record.ToString() == textBox1.Text
&& x.IPaddress == textBox2.Text
&& x.Machinename == textBox8.Text
&& x.username == textBox4.Text
&& x.password == textBox3.Text
&& x.eagleIOUsername == textBox9.Text
&& x.eagleIOpassword == textBox11.Text
&& x.sourcefolder == textBox7.Text
&& x.eagleIOHost == textBox12.Text
&& x.eagleIOdirectory == textBox13.Text
&& x.destfolder == textBox6.Text
&& x.filextension == textBox5.Text).FirstOrDefault();

var item = currentList.Where(m => m.Record == item1.Record + 1).FirstOrDefault();

textBox1.Text = item.Record.ToString();
textBox2.Text = item.IPaddress;
textBox8.Text = item.Machinename;
textBox4.Text = item.username;
textBox3.Text = item.password;
textBox7.Text = item.sourcefolder;
textBox6.Text = item.destfolder;
textBox5.Text = item.filextension;
textBox9.Text = item.eagleIOUsername;
textBox11.Text = item.eagleIOpassword;
textBox12.Text = item.eagleIOHost;
textBox13.Text = item.eagleIOdirectory;
}
else
{
currentList = new List<Datalogger>();
}

}


I highly appreciate your help, and thanks in advance.


Sami Arja

Continue reading...
 
Back
Top