Search code examples
c#microsoft-graph-apimicrosoft-graph-edu

Convert JSON response to Graph.EducationAssignment Object


When I attempt to convert a JSON response to a Graph.EducationAssignment root Object there is no data. I get no error message, just an empty object.

I'm calling /beta/education/classes/{classId}/assignments

HttpResponseMessage response = await HttpClient.GetAsync(webApiUrl);
if (response.IsSuccessStatusCode)
{
    string json = await response.Content.ReadAsStringAsync();

    Microsoft.Graph.EducationAssignment Assignments =
        JsonConvert.DeserializeObject<Microsoft.Graph.EducationAssignment>(json);

    // List<Microsoft.Graph.EducationAssignment> Assignments1 =
    //    JsonConvert.DeserializeObject<List<Microsoft.Graph.EducationAssignment>>(json);
}

Due to the size of the response, here is the first line, which shows that the assignments are held in an array.

{
  "@odata.context": "https://graph.microsoft.com/beta/$metadata#education/classes('id')/assignments",
  "value": [
    {
      "classId": "blah-blah",
      "displayName": "The homeless",
      "closeDateTime": null

I'd appreciate it if somebody could help me out.

enter image description here


Solution

  • You don't describe what Microsoft.Graph.EducationAssignment looks like or where it came from. The most likely issue is that you're trying to deserialize a collection of resources (which is contained in value) into a single object.

    Unless you're familiar with OData and JSON serialization, I'd avoid trying to hand-craft your own Graph client. You will have a much easier time using the Microsoft Graph Beta SDK. The SDK will handle all of this for you and you'll end up with cleaner code:

    var assignments = await graphClient
        .Education
        .Classes["id"]
        .Assignments
        .Request()
        .GetAsync();
    
    var assignment = await graphClient
        .Education
        .Classes["id"]
        .Assignments["id"]
        .Request()
        .GetAsync();