In my C# + WPF + .NET 4.5 code, suppose I have defined a Player class in the following manner:
public class Player {
public string FirstName;
public string LastName;
public List<int> Cells;
public string Level;
}
And I have a myobjects.json file in which I managed to write (using JSON.NET) a serialized collection of these objects (first two shown below):
{
"FirstName": "Foo",
"LastName": "Fighter",
"Cells": [
1,
2,
3
],
"Level": "46"
}{
"FirstName": "Bar",
"LastName": "Baz",
"Cells": [
104,
127,
],
"Level": "D2"
}
What I would like to do is to read the file, and deserialize these objects and populate a List of Players:
using (Stream fs = openDialog.OpenFile())
using (StreamReader sr = new StreamReader(fs))
using (JsonTextReader jr = new JsonTextReader(sr)) {
while (jr.Read()) {
/* Find player in file */
Player p = /* Deserialize */
PlayerList.Add(p);
}
}