【问题标题】:ASP.NET Core 3.0 JSON Serialisation IssueASP.NET Core 3.0 JSON 序列化问题
【发布时间】:2020-06-24 07:15:57
【问题描述】:

我正在将 ASP.NET Web API 转换为 ASP.NET Core API。

这是我的课,同样的代码在 WEB API 中工作。

public class TotalEquipments
{
    public string Totalresults { get; set; }
    public Equipment Equipment;
}

public class Equipment
{
    public string CardName { get; set; }
    public string CardCode { get; set; }
    public string ItemCode { get; set; }
    public string ItemName { get; set; }
    public string Serialno { get; set; }
    public string Status { get; set; }
}

预期输出:

[{"Equipment":{"CardName":"PT Syspex Kemasindo","CardCode":"C-USD-0032","ItemCode":"V0437-MH009-2002","ItemName":"Smipack Shrink Machine (Manual L Sealer), Model: S560","serialno":"00004183","status":"Active"},"totalresults":"1719"},{"Equipment":{"CardName":"PT Syspex Kemasindo","CardCode":"C-USD-0032","ItemCode":"V0402-MH010-0001","ItemName":"Automatic Strapping Machine, Model: TZ 700 (850x600)","serialno":"014437","status":"Active"},"totalresults":"1719"}]

我得到的输出:

[{"Totalresults":"1719"},{"Totalresults":"1719"}]

【问题讨论】:

  • 为 TotalEquipments 上的 Equipment 属性添加 getter 和 setter

标签: c# asp.net json asp.net-core


【解决方案1】:

.NET Core 3 中的 JSON 序列化程序(System.Text.Json 命名空间中的对象)不序列化 fields,仅序列化 properties。所以你只需要修复你的模型:

public class TotalEquipments
{
    public string Totalresults { get; set; }
    public Equipment Equipment { get; set; } // <--- This
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    • 2020-11-20
    相关资源
    最近更新 更多