How do I convert a JSON inner array to an array of objects?

  Kiến thức lập trình

I have tried the following code:

dynamic result = JsonConvert.DeserializeObject(response.Content!);
var jsonArray = result.items;
var arrayEvents = JsonConvert.DeserializeObject<VanEvent[]>(jsonArray);

To get an array of the objects in this JSON (response.Content):

{
  "items": [
    {
      "eventId": 408936,
      "name": "Door to door",
      "shortName": "door",
      "description": "walking the beat",
      "startDate": "2024-04-17T12:00:00-06:00",
      "endDate": "2024-04-17T15:30:00-06:00",
      "eventType": {
        "eventTypeId": 659466,
        "name": "Canvassing"
      },
      "isOnlyEditableByCreatingUser": false,
      "isPubliclyViewable": null,
      "locations": [],
      "codes": [],
      "notes": [],
      "shifts": [
        {
          "eventShiftId": 461796,
          "name": "Single Shift",
          "startTime": "2024-04-17T12:00:00-06:00",
          "endTime": "2024-04-17T15:30:00-06:00"
        }
      ],
      "roles": [
        {
          "roleId": 567170,
          "name": "Organizer",
          "isEventLead": false,
          "min": null,
          "max": null,
          "goal": null
        },
        {
          "roleId": 567171,
          "name": "Volunteer",
          "isEventLead": false,
          "min": null,
          "max": null,
          "goal": null
        }
      ],
      "ticketCategories": null,
      "voterRegistrationBatches": null,
      "createdDate": "2024-04-15T16:57:00Z",
      "districtFieldValue": null,
      "financialProgram": null,
      "isActive": true,
      "dotNetTimeZoneId": "Mountain Standard Time"
    },
    {
      "eventId": 422483,
      // ...

But the call to DeserializeObject() is throwing a RuntimeBinderException with a message of:

The best overloaded method match for 'Newtonsoft.Json.JsonConvert.DeserializeObject<ThirdPartyServices.VAN.VanEvent[]>(string)' has some invalid arguments

Why? This is passing in the array of these objects. And yes, VanEvent has a parameterless constructor.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT