json.net

JsonConvert.DeserializeObject get Task List returns Null


Been looking at this for a few hours now and am struggling to work out how to deserialize the response from a call to a Web API. We had a previous version that worked fine but the Endpoint structure changed after an update to API and as a result the code no longer works.

Basically I am trying to extract the Tasks returned from the results of the API call, see below for a structure of the returned API call from Postman.

{
  "Version": 1,
  "Result": "Success",
  "Data": {
    "Order": {
      "Oid": "f3a3afa9-ac2b-4914-b3cb-585c761f2dfb",
      "Did": 265532,
      "Tasks": [
        {
          "Oid": "af82ce50-0587-4752-ba3b-d60dbfebe041",
          "CreatedOn": "2023-05-19T02:24:55.437Z",
          "ChangedOn": "2023-05-19T02:45:26.28Z",
          "Description": "",
          "Comment": "",
          "IsStarted": false,
          "ScheduledOn": "2023-05-19T12:14:51.507",
          "ProductionDeadline": "2023-05-21T16:00:00",
          "ScheduledStart": "2023-05-19T11:56:00",
          "ScheduledEnd": "2023-05-19T12:04:00",
          "RealStart": "0001-01-01T00:00:00",
          "RealEnd": "0001-01-01T00:00:00",
          "OrderItemPartsCount": 27,
          "CalculatedSeconds": "120",
          "IdleSeconds": "0",
          "ScheduledSeconds": 480,
          "RealSeconds": 0,
          "PercentComplete": 0,
          "HasFinishedItems": false,
          "IsCustomTask": false,
          "AppointmentType": "OrderSpecificAppointment",
          "Station": {
            "_Value": "Centurio",
            "oid": "f7f3c5fe-49f0-4a9a-b366-29c4ab990fc7"
          },
          "MasterTask": {
            "_Value": "",
            "oid": "179212f4-ff0e-465d-962c-51b7da711bb7"
          }
        },
        {
          "Oid": "72b6a0ba-cbc2-4f9b-9372-86805d40f7e3",
          "CreatedOn": "2023-05-19T02:24:55.437Z",
          "ChangedOn": "2023-05-19T02:25:28.37Z",
          "Description": "",
          "Comment": "",
          "IsStarted": false,
          "ScheduledOn": "2023-05-19T11:54:55.437",
          "ProductionDeadline": "2023-05-21T16:00:00",
          "ScheduledStart": "2023-05-19T12:11:00",
          "ScheduledEnd": "2023-05-19T13:10:00",
          "RealStart": "0001-01-01T00:00:00",
          "RealEnd": "0001-01-01T00:00:00",
          "OrderItemPartsCount": 27,
          "CalculatedSeconds": "3540",
          "IdleSeconds": "0",
          "ScheduledSeconds": 3540,
          "RealSeconds": 0,
          "PercentComplete": 0,
          "HasFinishedItems": false,
          "IsCustomTask": false,
          "AppointmentType": "OrderSpecificAppointment",
          "Station": {
            "_Value": "Twinmatic",
            "oid": "d92a32c3-0763-49d7-a8eb-77ea68afa311"
          }
        }
      ]
    }
  },
  "Details": ""
}

In the previous version there was no "Order" element, can anyone assist in showing how I can get access to the Tasks as a List so I can process them?

Regards Peter.


Solution

  • You can use something like https://jsonutils.com to transform JSON into boilerplate code for deserializing your object.

    I used it over your JSON and that gave me a whole heap of classes and from there, I was able to put this very basic program together that does the deserialization (taking note that you may want to clean it up by changing variable names to conform to naming standards and also refactoring, etc.) ...

    using Newtonsoft.Json;
    
    namespace StackoverflowAnswer
    {
        public class Station
        {
            public string _Value { get; set; }
            public string oid { get; set; }
        }
    
        public class MasterTask
        {
            public string _Value { get; set; }
            public string oid { get; set; }
        }
    
        public class TaskResponse
        {
            public string Oid { get; set; }
            public DateTime CreatedOn { get; set; }
            public DateTime ChangedOn { get; set; }
            public string Description { get; set; }
            public string Comment { get; set; }
            public bool IsStarted { get; set; }
            public DateTime ScheduledOn { get; set; }
            public DateTime ProductionDeadline { get; set; }
            public DateTime ScheduledStart { get; set; }
            public DateTime ScheduledEnd { get; set; }
            public DateTime RealStart { get; set; }
            public DateTime RealEnd { get; set; }
            public int OrderItemPartsCount { get; set; }
            public string CalculatedSeconds { get; set; }
            public string IdleSeconds { get; set; }
            public int ScheduledSeconds { get; set; }
            public int RealSeconds { get; set; }
            public int PercentComplete { get; set; }
            public bool HasFinishedItems { get; set; }
            public bool IsCustomTask { get; set; }
            public string AppointmentType { get; set; }
            public Station Station { get; set; }
            public MasterTask MasterTask { get; set; }
        }
    
        public class Order
        {
            public string Oid { get; set; }
            public int Did { get; set; }
            public IList<TaskResponse> Tasks { get; set; }
        }
    
        public class Data
        {
            public Order Order { get; set; }
        }
    
        public class Response
        {
            public int Version { get; set; }
            public string Result { get; set; }
            public Data Data { get; set; }
            public string Details { get; set; }
        }
    
        internal class Program
        {
            static void Main(string[] args)
            {
                MainAsync().Wait();
            }
    
            static async Task MainAsync()
            {
                var json = "{\"Version\":1,\"Result\":\"Success\",\"Data\":{\"Order\":{\"Oid\":\"f3a3afa9-ac2b-4914-b3cb-585c761f2dfb\",\"Did\":265532,\"Tasks\":[{\"Oid\":\"af82ce50-0587-4752-ba3b-d60dbfebe041\",\"CreatedOn\":\"2023-05-19T02:24:55.437Z\",\"ChangedOn\":\"2023-05-19T02:45:26.28Z\",\"Description\":\"\",\"Comment\":\"\",\"IsStarted\":false,\"ScheduledOn\":\"2023-05-19T12:14:51.507\",\"ProductionDeadline\":\"2023-05-21T16:00:00\",\"ScheduledStart\":\"2023-05-19T11:56:00\",\"ScheduledEnd\":\"2023-05-19T12:04:00\",\"RealStart\":\"0001-01-01T00:00:00\",\"RealEnd\":\"0001-01-01T00:00:00\",\"OrderItemPartsCount\":27,\"CalculatedSeconds\":\"120\",\"IdleSeconds\":\"0\",\"ScheduledSeconds\":480,\"RealSeconds\":0,\"PercentComplete\":0,\"HasFinishedItems\":false,\"IsCustomTask\":false,\"AppointmentType\":\"OrderSpecificAppointment\",\"Station\":{\"_Value\":\"Centurio\",\"oid\":\"f7f3c5fe-49f0-4a9a-b366-29c4ab990fc7\"},\"MasterTask\":{\"_Value\":\"\",\"oid\":\"179212f4-ff0e-465d-962c-51b7da711bb7\"}},{\"Oid\":\"72b6a0ba-cbc2-4f9b-9372-86805d40f7e3\",\"CreatedOn\":\"2023-05-19T02:24:55.437Z\",\"ChangedOn\":\"2023-05-19T02:25:28.37Z\",\"Description\":\"\",\"Comment\":\"\",\"IsStarted\":false,\"ScheduledOn\":\"2023-05-19T11:54:55.437\",\"ProductionDeadline\":\"2023-05-21T16:00:00\",\"ScheduledStart\":\"2023-05-19T12:11:00\",\"ScheduledEnd\":\"2023-05-19T13:10:00\",\"RealStart\":\"0001-01-01T00:00:00\",\"RealEnd\":\"0001-01-01T00:00:00\",\"OrderItemPartsCount\":27,\"CalculatedSeconds\":\"3540\",\"IdleSeconds\":\"0\",\"ScheduledSeconds\":3540,\"RealSeconds\":0,\"PercentComplete\":0,\"HasFinishedItems\":false,\"IsCustomTask\":false,\"AppointmentType\":\"OrderSpecificAppointment\",\"Station\":{\"_Value\":\"Twinmatic\",\"oid\":\"d92a32c3-0763-49d7-a8eb-77ea68afa311\"}}]}},\"Details\":\"\"}";
    
                var deserializedObject = JsonConvert.DeserializeObject<Response>(json);
    
                // Break here            
            }
        }
    }