【发布时间】:2020-06-06 20:46:13
【问题描述】:
我对 .Net API 开发比较陌生,如果这里的答案很明显,我深表歉意。
我有 JSON 通过请求进入。它是一个带有几个子类的嵌套 JSON 结构。
我需要获取此 JSON,然后对其进行重组并将其分发到两个平面类中,以便存储在两个数据库表中。
解决此问题的最佳方法是什么?我在学习过程中使用了 DTO 和 Automapper,但我不确定如何使用嵌套结构来解决这个问题。
传入的 JSON 如下所示:
{
"id": xxx,
"parent_id": xxx,
"number": "xxx",
"order_key": "xxx",
"created_via": "xxx",
"version": "xxx",
"status": "xxx",
"currency": "xxx",
"date_created": "xxx",
"date_created_gmt": "xxx",
"date_modified": "xxx",
"date_modified_gmt": "xxx",
"discount_total": "xxx",
"discount_tax": "xxx",
"shipping_total": "xxx",
"shipping_tax": "xxx",
"cart_tax": "xxx",
"total": "xxx",
"total_tax": "xxx",
"prices_include_tax": xxx,
"customer_id": xxx,
"customer_ip_address": "xxx",
"customer_user_agent": "xxx",
"customer_note": "",
"billing": {
"first_name": "xxx",
"last_name": "xxx",
"company": "",
"address_1": "",
"address_2": "",
"city": "",
"state": "",
"postcode": "",
"country": "",
"email": "xxx",
"phone": "xxx"
},
"shipping": {
"first_name": "xxx",
"last_name": "xxx",
"company": "",
"address_1": "",
"address_2": "",
"city": "",
"state": "",
"postcode": "",
"country": ""
},
"payment_method": "xxx",
"payment_method_title": "xxx",
"transaction_id": "",
"date_paid": xxx,
"date_paid_gmt": xxx,
"date_completed": xxx,
"date_completed_gmt": xxx,
"cart_hash": "xxx",
"meta_data": [
{
"id": xxx,
"key": "xxx",
"value": "xxx"
}
],
"line_items": [
{
"id": xxx,
"name": "xxx",
"product_id": xxx,
"variation_id": xxx,
"quantity": xxx,
"tax_class": "",
"subtotal": "xxx",
"subtotal_tax": "xxx",
"total": "xxx",
"total_tax": "xxx",
"taxes": [],
"meta_data": [],
"sku": "",
"price": xxx
}
],
"tax_lines": [],
"shipping_lines": [],
"fee_lines": [],
"coupon_lines": [],
"refunds": [],
"_links": {
"self": [
{
"href": "xxx"
}
],
"collection": [
{
"href": "xxx"
}
],
"customer": [
{
"href": "xxx"
}
]
}
}
只有其中的某些部分是相关的。它需要被映射成2个类,如下:
public class OnlineOrderHeader
{
[Key]
[Required]
public int OnlineOrderID { get; set; }
[Required]
public int AppOrderID { get; set; }
public int OrderNumber { get; set; }
[MaxLength(50)]
public string OrderKey { get; set; }
[MaxLength(50)]
public string CreatedVia { get; set; }
[MaxLength(50)]
public string Version { get; set; }
[MaxLength(50)]
public string Status { get; set; }
[MaxLength(3)]
public string Currency { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
public decimal DiscountTotal { get; set; }
public decimal DiscountTax { get; set; }
public decimal CartTax { get; set; }
public decimal CartTotal { get; set; }
public int PriceIncludesTax { get; set; } //Check
public int CustomerID { get; set; }
[MaxLength(50)]
public string CustomerFirstName { get; set; }
[MaxLength(50)]
public string CustomerLastName { get; set; }
[MaxLength(50)]
public string CustomerEmail { get; set; }
[MaxLength(50)]
public string CustomerPhone { get; set; }
[MaxLength(50)]
public string CustomerEmployeeNo { get; set; }
[MaxLength(50)]
public string PaymentMethod { get; set; }
public int TransactionID { get; set; }
public DateTime DatePaid { get; set; }
[MaxLength(500)]
public string OrderURL { get; set; }
[MaxLength(500)]
public string CustomerNotes { get; set; }
[MaxLength(50)]
public string CuisineOrderStatus { get; set; }
}
和
public class OnlineOrderLines
{
[Key]
[Required]
public int OnlineOrderLineID { get; set; }
[Required]
public int AppOrderLineID { get; set; }
[Required]
public int ProductID { get; set; }
[MaxLength(50)]
public string SKU { get; set; }
public int Quantity { get; set; }
public decimal SubTotal { get; set; }
public decimal SubTotalTax { get; set; }
public decimal Money { get; set; }
[MaxLength(100)]
public string ProductDescription { get; set; }
[MaxLength(50)]
public string Category { get; set; }
}
我不确定如何使用 DTO 获取一个包含所有必要子类的对象。 一旦我有了那个对象,我认为将它分成几个类应该是相对简单的。
任何提示将不胜感激!
【问题讨论】:
标签: .net-core asp.net-core-webapi