我有以下结构的学生班:
public class Student
{
public int StudentId {get;set;}
public string StudentName {get;set;}
public string[] Courses {get;set;}
}
我有一个 student.json 文件,它保存了 Student 类的数据,如下所示:
public void SaveStudentData(Student stud, string path) //stud is an object of student class that has student information
{
string studinfo = JsonSerializer.SerializeToString<Student>(stud);
StreamWriter sw = File.OpenText(path);
sw.Write(studinfo);
}
但是,当我尝试使用以下代码检索数据时:
public Student GetStudentData(string path)
{
StreamReader sr = File.OpenText(path);
string stud = sr.ReadToEnd();
sr.Close();
Student studinfo =JsonSerializer.SerializeToString<Student>(stud);
return studinfo;
}
线
Student studinfo =JsonSerializer.SerializeToString<Student>(stud);
VS 2012 突出显示它无法将字符串类型转换为学生类型。我想将 json 数据从文件转换为学生类。我该怎么做?