Vigna 65ST
Vigna 65ST

Reputation: 31

ASP.NET Core 3.0 JSON Serialisation Issue

I am converting an ASP.NET Web API to ASP.NET Core API.

This is my class, the same code works in the 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; }
}

Expected OutPut:

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

Output i am getting:

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

Upvotes: 1

Views: 69

Answers (1)

DavidG
DavidG

Reputation: 118937

The JSON serialiser in .NET Core 3 (the objects in the System.Text.Json namespace) does not serialise fields, only properties. So you just need to fix your model:

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

Upvotes: 3

Related Questions