在.NET Core 2.1中格式化JSON响应。将多个属性移到单个枚举属性

时间:2018-07-12 13:14:04

标签: .net asp.net-core asp.net-core-2.0 asp.net-core-webapi

我想以此格式化我的JSON响应(已删除敏感数据):

{
    "id": 252,
    "name": "PRODUCT NAME",
    "manufacturer": "PRODUCT MANUFACTURER",
    "sku": "PRODUCT SKU",
    "ean": "PRODUCT EAN",
    "departmentID": 42,
    "department": "DEPARTMENT NAME",
    "categoryID": 178,
    "category": "CATEGORY NAME",
    "price": 0.00,
    "msrp": 0.00,
    "inactive": true,
    "stock": 0,
    "weight": 0,
    "imgFull": "IMAGE URL",
    "extendedDescription": "PRODUCT DESCRIPTION"
}

对此(部门和类别已移至第二维):

{
    "id": 252,
    "name": "PRODUCT NAME",
    "manufacturer": "PRODUCT MANUFACTURER",
    "sku": "PRODUCT SKU",
    "ean": "PRODUCT EAN",
    "Department": {
        "departmentID": 42,
        "department": "DEPARTMENT NAME"
    },
    "Category": {
        "categoryID": 178,
        "category": "CATEGORY NAME",
    },
    "price": 0.00,
    "msrp": 0.00,
    "inactive": true,
    "stock": 0,
    "weight": 0,
    "imgFull": "IMAGE URL",
    "extendedDescription": "PRODUCT DESCRIPTION"
}

这不仅使响应更整洁,而且我想使用该方法添加其他属性。例如,对于具有多个图像的产品,我想包括所有链接,而不仅仅是主要链接。

产品类别是这样:

public class apiProducts
{
    [Key]
    public Int32 Id { get; set; }
    [Required]
    public String Name { get; set; }
    [Required]
    public String Manufacturer { get; set; }
    [Required]
    public String SKU { get; set; }
    [Required]
    public String EAN { get; set; }
    [Required]
    public Int32 DepartmentID { get; set; }
    [Required]
    public String Department { get; set; }
    [Required]
    public Int32 CategoryID { get; set; }
    [Required]
    public String Category { get; set; }
    [Required]
    public Decimal Price { get; set; }
    [Required]
    public Decimal MSRP { get; set; }
    [Required]
    public Boolean Inactive { get; set; }
    [Required]
    public Int32 Stock { get; set; }
    [Required]
    public Double Weight { get; set; }
    [Required]
    public String ImgFull { get; set; }
    [Required]
    public String ExtendedDescription { get; set; }
}

由于我对.NET Core的有限了解,JSON对象是直接从类格式化的,所以我不确定如何使该类将多个属性推送到Enum中以按要求对其进行格式化。这可能很简单(我希望如此),但是我不知道该使用哪个搜索词来找到答案:(很抱歉,如果其他地方都已经回答了这个问题。

1 个答案:

答案 0 :(得分:0)

结果证明这很简单。首先,我需要新类别/部门字段的对象:

public class apiProductDepartments
{
    [Key]
    public Int32 ID { get; set; }
    [Required]
    public String Name { get; set; }
}
public class apiProductCategories
{
    [Key]
    public Int32 ID { get; set; }
    [Required]
    public String Name { get; set; }
}

我们将这些添加到主要产品类别,并将注释[IgnoreDataMember]添加到原始的DepartmentIDDepartmentName属性(与CategoryIDCategoryName相同) )从System.Runtime.Serialization命名空间中删除,以将其从json响应中删除(我们不需要两次使用属性,因此可以从json中删除它们):

public class apiProducts
{
    [Key]
    public Int32 Id { get; set; }
    [Required]
    public String Name { get; set; }
    [Required]
    public String Manufacturer { get; set; }
    [Required]
    public String SKU { get; set; }
    [Required]
    public String EAN { get; set; }
    public apiProductDepartments Department { get; set; }
    [IgnoreDataMember]
    [Required]
    public Int32 DepartmentID { get; set; }
    [IgnoreDataMember]
    [Required]
    public String DepartmentName { get; set; }
    public apiProductCategories Category { get; set; }
    [IgnoreDataMember]
    [Required]
    public Int32 CategoryID { get; set; }
    [IgnoreDataMember]
    [Required]
    public String CategoryName { get; set; }
    [Required]
    public Decimal Price { get; set; }
    [Required]
    public Decimal MSRP { get; set; }
    [Required]
    public Boolean Inactive { get; set; }
    [Required]
    public Int32 Stock { get; set; }
    [Required]
    public Double Weight { get; set; }
    [Required]
    public String ImgFull { get; set; }
    [Required]
    public String ExtendedDescription { get; set; }
}

然后,我们使用现有属性填充该属性:

foreach(apiProducts p in results)
            {
                apiProductDepartments dept = new apiProductDepartments();
                apiProductCategories cat = new apiProductCategories();
                dept.ID = p.DepartmentID;
                dept.Name = p.DepartmentName;
                cat.ID = p.CategoryID;
                cat.Name = p.CategoryName;
                p.Department = dept;
                p.Category = cat;
            }

然后我们得到正确格式的响应:

{
    "id": PRODUCT ID,
    "name": "PRODUCT NAME",
    "manufacturer": "PRODUCT MANUFACTURER",
    "sku": "PRODUCT SKU",
    "ean": "PRODUCT EAN",
    "department": {
        "id": DEPARTMENT ID,
        "name": "DEPARTMENT NAME"
    },
    "category": {
        "id": CATEGORY ID,
        "name": "CATEGORY NAME"
    },
    "price": PRICE,
    "msrp": MSRP,
    "inactive": false,
    "stock": STOCK,
    "weight": WEIGHT,
    "imgFull": "IMAGE LINK",
    "extendedDescription": "EXTENDED DESC"
}

整洁!