简体   繁体   English

如何反序列化 C#.Net Core 中的分页 object

[英]How do I deserialize a paging object in C# .Net Core

I have a string (stored in Redis) and I need to deserialize it to an object like this:我有一个字符串(存储在 Redis 中),我需要将其反序列化为 object,如下所示:

public class PagedList<T>
{
    public int TotalItems { get; }
    public int PageNumber { get; }
    public int PageSize { get; }
    public List<T> List { get; }

    //public PagedList() { }

    [JsonConstructor]
    public PagedList(IEnumerable<T> source, int totalRecord, int pageNumber, int pageSize)
    {
        TotalItems = totalRecord;
        PageNumber = pageNumber;
        PageSize = pageSize;
        List = source.ToList();
    }

    //[JsonConstructor]
    public PagedList(IQueryable<T> source, int totalRecord, int pageNumber, int pageSize)
    {
        TotalItems = totalRecord;
        PageNumber = pageNumber;
        PageSize = pageSize;
        List = source.ToList();
    }

    public int TotalPages => (int)Math.Ceiling(this.TotalItems / (double)this.PageSize);

    public PagingHeader GetHeader()
    {
        return new PagingHeader(this.TotalItems, this.PageNumber, this.PageSize, this.TotalPages);
    }
}

The string is:字符串是:

{
  "TotalItems": 63,
  "PageNumber": 1,
  "PageSize": 2147483646,
  "List": [
    {
      "Id": 6,
      "Tendvhc": "Tỉnh Lào Cai",
      "Parentid": null,
      "Matinh": "10",
      "Mahuyen": "000",
      "Maxa": "00000",
      "Status": 1
    }
  ]
}

I'm using this in my code, but getting error:我在我的代码中使用它,但出现错误:

JsonConvert.DeserializeObject<T>(jsonString)

Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type GIS.CQRS.Queries.PagedList`1[iNRES.Service.Category.Domain.Models.Dvhc]. Newtonsoft.Json.JsonSerializationException:找不到用于类型 GIS.CQRS.Queries.PagedList`1[iNRES.Service.Category.Domain.Models.Dvhc] 的构造函数。 A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. class 应该有一个默认构造函数,一个具有 arguments 的构造函数或一个标有 JsonConstructor 属性的构造函数。 Path 'TotalItems', line 1, position 14. at Newtonsoft.Json.Serialization.JsonS路径“TotalItems”,第 1 行,position 14. 在 Newtonsoft.Json.Serialization.JsonS

How can I deal with complex object like above?我该如何处理像上面这样复杂的 object?

I have just fixed this problem by:我刚刚通过以下方式解决了这个问题:

using使用

using JsonConstructorAttribute = Newtonsoft.Json.JsonConstructorAttribute;

instead of代替

using System.Text.Json.Serialization;

and adding this constructor并添加此构造函数

[JsonConstructor]
public PagedList(List<T> list, int totalItems, int pageNumber, int pageSize)
{
    this.TotalItems = totalItems;
    this.PageNumber = pageNumber;
    this.PageSize = pageSize;
    this.List = list;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 C# .NET 中反序列化复杂的 JSON 对象? - How do I deserialize a complex JSON object in C# .NET? 如何反序列化为现有对象 - C# - How do I deserialize into an existing object - C# 当使用Full .net Framework序列化初始对象时,如何将二进制数据反序列化为.net Core中的对象? - How do I deserialize binary data into an object in .net Core, when the initial object was serialized with Full .net Framework? 如何在C#(。net核心)中将对象数组转换为JSON对象 - How Do I convert my array of objects into JSON object in C# (.net core) 使用 C# .NET 内核反序列化 json - Deserialize json with C# .NET Core 如何基于行数据在ac#.NET gridview中设置分页? - How do I set up paging in a c# .NET gridview based on row data? 在C#中,如何将XML从较旧的对象反序列化为更新的对象并忽略缺少的xml元素? - In C# how do I deserialize XML from an older object into the updated object and ignore missing xml elements? C#Protobuf-net:如何从网络流中连续反序列化? - C# Protobuf-net: how do I deserialize consecutively from a network stream? C# 如何正确地将数组中的多个自定义对象反序列化为一个对象? - C# How do I correctly deserialize multiple custom objects in an array to an object? 如果json对象的名称以int开头,如何将json反序列化为c#模型 - How do I deserialize json to c# model if the json object has a name starting with an int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM