将 json 反序列化为抛出错误的对象列表。无法反序列化当前 JSON 对象(例如 {"name":"value"})

Deserilizing the json to list of an object throwing error. Cannot deserialize the current JSON object (e.g. {"name":"value"})

我正在尝试将 Json 反序列化为 Student 的 List 对象,该对象由 studentName 和 studentId 组成。我确实得到了大约 200 名学生的 jsonResponse,但是当我开始反序列化时,出现了以下错误。我确实针对此错误进行了研究,该问题的修复与我拥有的代码类似,因此我不确定哪里出了问题。

无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[MyApp.Models.Student]',因为该类型需要一个 JSON 数组(例如 [1, 2,3]) 正确反序列化。

public static async Task<List<Student>> GetUserInfo()
{
    var token = await AccessToken.GetGraphAccessToken();
    // Construct the query
    HttpClient client = new HttpClient();
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, Globals.MicrosoftGraphUsersApi);
    request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

    // Ensure a successful response
    HttpResponseMessage response = await client.SendAsync(request);
    response.EnsureSuccessStatusCode();

    // Populate the data store with the first page of groups
    string jsonResponse = await response.Content.ReadAsStringAsync();
    var students = JsonConvert.DeserializeObject<List<Student>>(jsonResponse);

    return students;   
}

下面是来自 Microsoft Graph Api

的 JSON 响应
{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users(studentName,studentId)",
  "value": [
    {"studentName":"Radha,NoMore","studentId":"420"},
    {"studentName":"Victoria, TooMuch","studentId":"302"}
  ]
}

C# 同学Class:

public class Student
{
    public string studentName { get; set; } 
    public string studentId { get; set; }
}

JSON 响应包含 value: 属性,属性 包含学生数组数据。因此,您需要制作一个额外的 class,其中包含 List<Student> value 属性,反序列化为 class,然后您可以使用value属性,如下:

var listHolder = JsonConvert.DeserializeObject<StudentListHolder>(jsonResponse);
var list = listHolder.value;
foreach (var student in list)
{
    Console.WriteLine(student.studentId + " -> " + student.studentName);
}

这是附加的class:

public class StudentListHolder // pick any name that makes sense to you
{
    public List<Student> value { get; set; }
}

工作演示 (.NET Fiddle):https://dotnetfiddle.net/Lit6Er