在我的C# + WPF + .NET 4.5代码中,假设我Player
以以下方式定义了一个类:
public class Player {
public string FirstName;
public string LastName;
public List<int> Cells;
public string Level;
}
我有一个myobjects.json文件,我设法在其中编写(使用JSON.NET)这些对象的序列化集合(前两个如下所示):
{
"FirstName": "Foo",
"LastName": "Fighter",
"Cells": [
1,
2,
3
],
"Level": "46"
}{
"FirstName": "Bar",
"LastName": "Baz",
"Cells": [
104,
127,
],
"Level": "D2"
}
我想做的是读取文件,并反序列化这些对象并填充 a List
of Player
s:
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);
}
}