从 RestSharp JsonConvert DeserializeObject 获取特定值并保存到数据库

问题描述 投票:0回答:0

我需要从 DeserializeObject 访问特定值并保存到数据库。 这是 restsharp api 设置的示例。

string url = "sample_url";
string key = "sample_key";
string secret = "sample_secret";  

var client = new RestClient(url);
var request = new RestRequest("/wp-json/wc/v3/orders", Method.Get);

request.AddParameter("consumer_key", key);
request.AddParameter("consumer_secret", secret);
request.AddParameter("format", "json");

var response = await client.ExecuteGetAsync(request);
var wooOrders = JsonConvert.DeserializeObject<List<WooOrderModel>>(response.Content);

这是模型。

public class WooOrderModel
{
    public int Id { get; set; }
    public string Status { get; set; }
    public int Customer_Id { get; set; }
    public Billing Billing { get; set; }
    public Shipping Shipping { get; set; }
    public List<Line_Items> Line_Items { get; set; }
}

public class Billing
{
    public string First_Name { get; set; } 
    public string Last_Name { get; set; }
    public string Company { get; set; }
    public string Address_1 { get; set; }
    public string Address_2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Country { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
}

public class Shipping
{
    public string First_Name { get; set; } 
    public string Last_Name { get; set; }
    public string Company { get; set; }
    public string Address_1 { get; set; }
    public string Address_2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Country { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
}

public class Line_Items
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Product_Id { get; set; }
    public int Quantity { get; set; }
}

这是上面代码的示例 json 结果。

{
  "result": [
    {
      "id": 1,
      "status": "pending",
      "customer_Id": 1,
      "billing": {
        "first_Name": "Test",
        "last_Name": "Test",
        "company": "",
        "address_1": "Test",
        "address_2": "",
        "city": "Test",
        "state": "00",
        "country": "TS",
        "email": "[email protected]",
        "phone": "9123456"
      },
      "shipping": {
        "first_Name": "",
        "last_Name": "",
        "company": "",
        "address_1": "",
        "address_2": "",
        "city": "",
        "state": "",
        "country": "",
        "email": null,
        "phone": ""
      },
      "line_Items": [
        {
          "id": 47,
          "name": "Product",
          "product_Id": 4321,
          "quantity": 1
        }
      ]
    },
    {
      "id": 2,
      "status": "pending",
      "customer_Id": 1,
      "billing": {
        "first_Name": "Test",
        "last_Name": "Test",
        "company": "",
        "address_1": "Test",
        "address_2": "",
        "city": "Test",
        "state": "00",
        "country": "TS",
        "email": "[email protected]",
        "phone": "9123456"
      },
      "shipping": {
        "first_Name": "",
        "last_Name": "",
        "company": "",
        "address_1": "",
        "address_2": "",
        "city": "",
        "state": "",
        "country": "",
        "email": null,
        "phone": ""
      },
      "line_Items": [
        {
          "id": 46,
          "name": "Product 2",
          "product_Id": 1234,
          "quantity": 1
        }
      ]
    }
  ]
}

我需要保存枚举项,我提供了一些我需要的过程。任何想法我将如何有效地实现这一点?

需要保存的项目:

  • 身份证
  • 状态
  • Customer_Id
  • 账单.电子邮件
  • 送货.Email
  • Line_Items.Name
  • Line_Items.Product_Id
  • Line_Items.Quantity
var order = new Order
{
    OrderId = Id
    Status = Status
    CustomerId = Customer_Id,
    BillingEmail = Billing.Email,
    ShippingEmail = Shipping.Email,
    LineItemName = Line_Items.Name,
    LineItemProductId = Line_Items.Product_Id,
    LineItemQuantity = Line_Items.Quantity 
};

await _context.Orders.AddAsync(order, cancellationToken);
await _context.SaveChangesAsync(cancellationToken);

我对此有点陌生,将不胜感激。谢谢

c# .net entity-framework restsharp woocommerce-rest-api
© www.soinside.com 2019 - 2024. All rights reserved.