我有下面的類反序列化:問題使用Newtonsoft JSON
public class Student
{
public int studentNumber;
public string testWeek;
public string topics;
}
我做一些東西給它,序列化並將其保存在一個文件中。它看起來像這樣:
[
{
"studentNumber": 1,
"testWeek": "1",
"topics": "5 & 8"
},
{
"studentNumber": 2,
"testWeek": "1",
"topics": "5 & 8"
},
{
"studentNumber": 3,
"testWeek": "1",
"topics": "5 & 8"
},
{
"studentNumber": 4,
"testWeek": "1",
"topics": "5 & 8"
},
{
"studentNumber": 5,
"testWeek": "1",
"topics": "5 & 8"
}
]
後來我想反序列化它,所以我可以再次工作。我有這個代碼
Student[] arr = new Student[numberOfStudentsInClass];
arr = JsonConvert.DeserializeObject<Student>(File.ReadAllText(_selectedClass))
其中_selectedClass是包含文件名的字符串。但我得到一個錯誤
無法轉換WindowsFormApplicationsForm1.Form.Student到WindowsFormApplicationsForm1.Form.Student []
謝謝。我以前一直在將數據作爲一個數組進行處理,但也許列表可能會更好。時間開始重寫! – Luves2spooge