【问题标题】:Working with nested JSON request data in .Net Core API在 .Net Core API 中处理嵌套的 JSON 请求数据
【发布时间】: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


    【解决方案1】:

    首先,确保您的 JSON 有效。上面那个不是。我认为数据类型需要更加明显。你有"xxx"xxx - 后者应该是我假设的数字?无论如何,我通常只使用字符串作为原语,稍后再担心数值数据类型。

    一旦您拥有一个有效的 JSON(即从请求中获取响应字符串并删除转义字符 - 使用 regex 轻松完成),您可以将其粘贴到在线 JSON 到 C# 转换器(如 this one)。

    这应该给你一个类结构。您可以将 RootObject 重命名为 API 响应类的主名称。我还检查了原始数据类型,因为这些转换器并不完美。正如我上面提到的,当映射到视图模型/实体时,我通常将原语更改为字符串并稍后处理数据类型。

    然后,在您的 API 调用类中:

    1. 发出 API 请求(即httpClient.GetAsync(requestUrl)
    2. 以字符串形式读取响应(即response.ReadAsStringAsync()
    3. 使用Newtonsoft.JSON 对其进行反序列化。
    var response = httpClient.GetAsync(requestUrl);
    var json = await response.ReadAsStringAsync();
    var result = JsonConvert.DeserializeObject<RootObject>(json);
    

    一旦你有了 RootObject 模型(这是一个反序列化为对象的原始 API 响应),你可以使用前面提到的 Automapper 创建 OnlineOrderHeaderOnlineOrderLine 查看模型(简化/展平)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-03
      • 2021-08-21
      相关资源
      最近更新 更多