Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

I think you are getting this error because JsonConvert.DeserializeObject<T>() expects a string, but you are passing it an object from the previous deserialization, probably a JArray.

Try this instead:

var result = JObject.Parse(response.Content!);
var arrayEvents = result["items"].ToObject<VanItem[]>();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading