无法使用 .NET Core 反序列化 JSON 字符串

Cannot deserialize a JSON string using .NET Core

我最近切换到 .NET Core,但在将以下 JSON 字符串反序列化到此对象时遇到问题。通常使用 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; }
}

这是 JSON 刺痛:

{
  "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
}

错误信息如下:

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

搞笑的是Visual Studio在查看报错数据时将JSON字符串解析为JSON没有问题所以一定是可以的!

问题可能出在列表 indexEventVMs 中 JSON 列表是 [] 但在示例中它是一个对象 {}。

所以根据您的 JSON 类 应该是这样的:

    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; }
    }

如果您需要 indexEventVMs 接收列表,您需要将 json object {} 更改为包含 object[{}] 的数组。

你必须更换

public List<indexEventVMLite> indexEventVMs { get; set; }

有了这个

public indexEventVMLite indexEventVMs { get; set; }

但最好试试这个代码


    var json = ...your json
    var result = JsonConvert.DeserializeObject<smDesktopSearchResultsVM>(json);
    var resultSerialized =JsonConvert.SerializeObject(result);

结果

{"page":1,"totalRecs":11,"totalPages":2,"indexEventVMs":{"Event":{"WebsiteId":5140,"EventId":14614,"EventName":"Proofpix Elementary School","EventPassword":"proofpixelementarydemo","EventDate":"2021-08-30T16:00:00-02:30","EventEndDate":"2021-08-30T20:00:00-02:30","EventExpires":"2022-09-01T05:00:00-02:30","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/Descendant/1939278/680_9099_class-composite-7a.jpg","retinaPageImage":null,"linkId":354967,"linkgroupId":9527,"pageURL":"https://jackblack.proofpix.com/proofpix-elementary-school"},"LinkGroupId":9527}

    public partial class  smDesktopSearchResultsVM
    {
        [JsonProperty("page")]
        public long Page { get; set; }

        [JsonProperty("totalRecs")]
        public long TotalRecs { get; set; }

        [JsonProperty("totalPages")]
        public long TotalPages { get; set; }

        [JsonProperty("indexEventVMs")]
        public IndexEventVMs IndexEventVMs { get; set; }

        [JsonProperty("LinkGroupId")]
        public long LinkGroupId { get; set; }
    }

    public partial class IndexEventVMs
    {
        [JsonProperty("Event")]
        public Event Event { get; set; }

        [JsonProperty("orderCount")]
        public long OrderCount { get; set; }

        [JsonProperty("sortOrder")]
        public long SortOrder { get; set; }

        [JsonProperty("pageImage")]
        public Uri PageImage { get; set; }

        [JsonProperty("retinaPageImage")]
        public object RetinaPageImage { get; set; }

        [JsonProperty("linkId")]
        public long LinkId { get; set; }

    [JsonProperty("linkgroupId")]
    public long LinkgroupId { get; set; }

    [JsonProperty("pageURL")]
    public Uri PageUrl { get; set; }
}

public partial class Event
{
    [JsonProperty("WebsiteId")]
    public long WebsiteId { get; set; }

    [JsonProperty("EventId")]
    public long EventId { get; set; }

    [JsonProperty("EventName")]
    public string EventName { get; set; }

    [JsonProperty("EventPassword")]
    public string EventPassword { get; set; }

    [JsonProperty("EventDate")]
    public DateTimeOffset EventDate { get; set; }

    [JsonProperty("EventEndDate")]
    public DateTimeOffset EventEndDate { get; set; }

    [JsonProperty("EventExpires")]
    public DateTimeOffset EventExpires { get; set; }

    [JsonProperty("DiscontinuedDate")]
    public object DiscontinuedDate { get; set; }

    [JsonProperty("forceDateDeleted")]
    public object ForceDateDeleted { get; set; }

    [JsonProperty("EventReady")]
    public bool EventReady { get; set; }
}