Search code examples
.net-corejson-deserialization

Cannot deserialize a JSON string using .NET Core


I have recently switched to .NET Core and I am having trouble deserializing the following JSON string into this object. Usually works like a charm using Newtonsoft.

public class smDesktopSearchResultsVM
{
    public smDesktopSearchResultsVM()
    {
        this.indexEventVMs = new List<indexEventVMLite>();
    }

    public int page { get; set; }
    public int totalRecs { get; set; }
    public int totalPages { get; set; }
    public int? LinkGroupId { get; set; }
    public List<indexEventVMLite> indexEventVMs { get; set; }
}

public class indexEventVMLite
{
    public indexEventVMLite()
    {
        this.Event = new EventVMLite();
    }

    public EventVMLite Event { get; set; }
    public int orderCount { get; set; }
    public int sortOrder { get; set; }
    public string pageImage { get; set; }
    public string retinaPageImage { get; set; }
    public int linkId { get; set; }
    public int linkgroupId { get; set; }
    public string pageURL { get; set; }
    
}

public class EventVMLite
{
    public int WebsiteId { get; set; }
    public int EventId { get; set; }
    public string EventName { get; set; }
    public string EventPassword { get; set; }
    public DateTime EventDate { get; set; }
    public DateTime? EventEndDate { get; set; }
    public DateTime? EventExpires { get; set; }
    public DateTime? DiscontinuedDate { get; set; }
    public DateTime? forceDateDeleted { get; set; }
    public bool EventReady { get; set; }
}

Here is the JSON sting:

{
  "page": 1,
  "totalRecs": 11,
  "totalPages": 2,
  "indexEventVMs": {
    "Event": {
      "WebsiteId": 5140,
      "EventId": 14614,
      "EventName": "Proofpix Elementary School",
      "EventPassword": "proofpixelementarydemo",
      "EventDate": "2021-08-30T16:00:00",
      "EventEndDate": "2021-08-30T20:00:00",
      "EventExpires": "2022-09-01T05:00:00",
      "DiscontinuedDate": null,
      "forceDateDeleted": null,
      "EventReady": true
    },
    "orderCount": 5,
    "sortOrder": 1,
    "pageImage": "https://s3.us-east-1.wasabisys.com/usstandard.cdn.proofpix.com/websites/5140/PageMedia/266450/Descendants/1939278/680_9099_class-composite-7a.jpg",
    "retinaPageImage": null,
    "linkId": 354967,
    "linkgroupId": 9527,
    "pageURL": "https://jackblack.proofpix.com/proofpix-elementary-school/"
  },
  "LinkGroupId": 9527
}

Here is the error message:

The JSON value could not be converted to System.Collections.Generic.List`1[SortMagic_Desktop.indexEventVMLite]. Path: $.indexEventVMs | LineNumber: 0 | BytePositionInLine: 57.

enter image description here

What is funny is that Visual Studio has no problem parsing the JSON string to JSON when viewing the error data so it must be possible!

enter image description here


Solution

  • The problem can be in the List indexEventVMs a JSON list is [] but in the example it's a object {}.

    So according to your JSON the classes would be something like this:

        public class Object
        {
            public long Page { get; set; }
            public long TotalRecs { get; set; }
            public long TotalPages { get; set; }
            public IndexEventVMs IndexEventVMs { get; set; }
            public long LinkGroupId { get; set; }
        }
    
        public class IndexEventVMs
        {
            public Event Event { get; set; }
            public long OrderCount { get; set; }
            public long SortOrder { get; set; }
            public Uri PageImage { get; set; }
            public object RetinaPageImage { get; set; }
            public long LinkId { get; set; }
            public long LinkgroupId { get; set; }
            public Uri PageUrl { get; set; }
        }
    
        public class Event
        {
            public long WebsiteId { get; set; }
            public long EventId { get; set; }
            public string EventName { get; set; }
            public string EventPassword { get; set; }
            public DateTimeOffset EventDate { get; set; }
            public DateTimeOffset EventEndDate { get; set; }
            public DateTimeOffset EventExpires { get; set; }
            public object DiscontinuedDate { get; set; }
            public object ForceDateDeleted { get; set; }
            public bool EventReady { get; set; }
        }
    

    If you need that indexEventVMs receive a list you need to change from the json object {} to an array with object[{}].