Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net core 在.Net核心API中使用嵌套的JSON请求数据_.net Core_Asp.net Core Webapi - Fatal编程技术网

.net core 在.Net核心API中使用嵌套的JSON请求数据

.net core 在.Net核心API中使用嵌套的JSON请求数据,.net-core,asp.net-core-webapi,.net Core,Asp.net Core Webapi,我对.NETAPI开发相对较新,因此,如果答案显而易见,我深表歉意 我收到一个请求。它是一个带有几个子类的嵌套JSON结构 我需要得到这个JSON,然后将其重新构造并分发到两个平面类中,以存储在两个数据库表中 最好的办法是什么?我在学习过程中使用过DTO和Automapper,但我不知道如何使用嵌套结构实现这一点 传入的JSON如下所示: { "id": xxx, "parent_id": xxx, "number": "xxx", "order_key": "x

我对.NETAPI开发相对较新,因此,如果答案显而易见,我深表歉意

我收到一个请求。它是一个带有几个子类的嵌套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; }
    }

我不知道如何使用DTO获得一个包含所有必要子类的对象。 一旦我有了那个对象,我想把它分成几个类应该是比较困难的


任何提示都将不胜感激

首先,确保您的JSON有效。上面的那个不是。我认为数据类型需要更加明显。您有
“xxx”
xxx
-我想后者应该是数字?无论如何,我通常只使用字符串作为基本体,并担心以后的数字数据类型

一旦您拥有了一个有效的JSON(即从请求中获取响应字符串并删除转义字符-很容易完成),您就可以将其粘贴到在线JSON到C#转换器(如)

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

然后,在API调用类中:

  • 发出该API请求(即
    httpClient.GetAsync(requestUrl)
  • 以字符串形式读取响应(即
    response.ReadAsStringAsync()
  • 使用
    Newtonsoft.JSON
    对其进行反序列化
  • var response=httpClient.GetAsync(requestUrl);
    var json=await response.ReadAsStringAsync();
    var result=JsonConvert.DeserializeObject(json);
    
    一旦拥有了
    RootObject
    模型(反序列化为对象的原始API响应),就可以使用前面提到的
    Automapper
    创建
    OnlineOrderHeader
    OnlineOrderLine
    视图模型(简化/展平)

    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; }
        }
    
    var response = httpClient.GetAsync(requestUrl);
    var json = await response.ReadAsStringAsync();
    var result = JsonConvert.DeserializeObject<RootObject>(json);