Reputation: 926
I'm able to write to a text file a list of objects with the JSON serializer, however my aim is to populate a list on demand from the json text file. I'm a little puzzled as how to do this. I'm not experienced with JSON at all and have only been playing around with it today for the first time.
Here is the code I use to write my list of ints to the JSON.
class LockTime
{
public int Start { get; set; }
public int End {get; set; }
public LockTime (int start, int end)
{
Start = start;
End = end;
}
}
This is the class where I'm writing to the JSON file (or the part where I'm doing it)
private List<LockTime> times;
int timeStart = pickerTimeStart.Value.Hour + pickerTimeStart.Value.Minute;
int timeEnd = pickerTimeEnd.Value.Hour + pickerTimeEnd.Value.Minute;
LockTime bla = new LockTime(timeStart, timeEnd);
times.Add(theTime);
string meh = JsonConvert.SerializeObject(times, Formatting.Indented);
File.WriteAllText(@filePath, meh);
There are 2 things I want to be able to do. The first is to populate a listview from the json file. In pseudo code
Deserialize the file with something like times = JsonConvert.DeserializeObject>(json);
but I can't quite wrap my head around how to go from the text file straight to the array and then how to display it in the listview. Before I was working with JSON I was working with a straight forward text file where I was firstly drawing a bunch of ints row by row and then displaying them in the listview via lstTime.Items.AddRange(GetTimes().Select(t => new ListViewItem(t)).ToArray());
Any idea how?
Upvotes: 0
Views: 245
Reputation: 4996
Unless I am mistaken, the problem here involves using JSON.NET serialization and deserialization. Here are some good SO resources on that:
Deserializing JSON data to C# using JSON.NET
The following will serialize and then deserialize the List that you have, and then retrieve a list of integers from those objects:
// Assume there is some list of LockTime objects.
List<LockTime> listOfTimes = new List<LockTime>
{
new LockTime(1, 5),
new LockTime(10, 15)
};
// Serialize this list into a JSON string.
string serializedList = JsonConvert.SerializeObject(listOfTimes, Formatting.Indented);
// You can write this out to a file like this:
//File.WriteAllText("path/to/json/file", serializedList);
// You can then read it back in, like this:
//var serializedList = File.ReadAllText("path/to/json/file")
// To get a list of integers, you can do:
List<LockTime> deserializedList = (List<LockTime>)JsonConvert.DeserializeObject(serializedList, typeof(List<LockTime>));
// Grab whatever data you want from this list to store somewhere, such as a list of all Start integers.
List<int> intList = deserializedList.Select(entry => entry.Start).ToList();
You can do whatever you want with the list of LockTime objects once you have them deserialized back into memory. I'm lacking details on the List View part, but from the line you provided in the OP, I'm guessing you can use the 'intList' above like this:
lstTime.Items.AddRange(intList.Select(t => new ListViewItem(t.ToString(CultureInfo.InvariantCulture))));
Upvotes: 1