How to iterate through a JSON file and move through the string by clicking on a button?

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

samiarja

Guest
I am building a windows form application in C# that takes user input and save it to JSON file, each user will be on a separate string like below as an example from the code: (I have input random data)

[
{
"Record": 1,
"IPaddress": "168.192.45.34",
"Machinename": "taurus",
"username": "root",
"password": "root",
"sourcefolder": "..../..../....",
"destfolder": "..../..../....",
"filextension": "db",
"removedownloaded": 1
},
{
"Record": 2,
"IPaddress": "168.192.45.255",
"Machinename": "nautitech",
"username": "root",
"password": "root",
"sourcefolder": "..../..../....",
"destfolder": "..../..../....",
"filextension": "txt",
"removedownloaded": 0
}]

Now each Record will have a number and It will be in a descending order. Each string is linked to a textboxes. As an example below, this is how I add data to the JSON file: (Read JSON file, deserialize it, create a new string, save new data, serialize and write all data to the file)

private void button4_Click(object sender, EventArgs e)
{
int count = 0;
if (MessageBox.Show("Are you sure you want to Add", "Add", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{

string filePath = @"C:\Users\Sami\Desktop\Companies\Nautitech Mining Systems Pty Ltd\Code\JSON\app-db.json";
string text = File.ReadAllText(filePath);
int v = checkBox1.Checked ? 1 : 0;

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,
removedownloaded = v


};


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_Add = Newtonsoft.Json.JsonConvert.SerializeObject(currentList, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine(output_Add);
File.WriteAllText(filePath, output_Add);
}
else
{
this.Activate();
}


}

In the application there is Forward, Backward, Last, First. When the user click on one of this button, the data from the JSON file will be displayed on the textbox on the application and then the user will have an option to delete, update or add new data. Those are the button that I wish the user to user to see the data.

So far I have figure out First and last, as they are easy to program. However, I am struggling in Forward, Backward. As this button will have to iterate through the JSON string and have to work everytime the user click on them until they reach the beginning or the end of the string.

Any Thoughts on how to solve that? I appreciate your time and your advice.

Continue reading...
 
Back
Top