You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将RestSharp返回的API响应数据映射到预定义JSON实体类?

问题描述

我是API、RestSharp和JSON的新手,调用Paymongo API后获取了响应数据,目前遇到的问题是不知道如何将RestSharp返回的响应数据填充到预先定义好的JSON实体类中。我的期望是将RestSharp响应内容填充到Attributes类中。

相关内容

1. API响应数据

{"data": {"id": "link_4txtnKwrBbTTQfRwswSLcinw","type": "link","attributes": {"amount": 65656,"archived": false,"currency": "USD","description": "2323","livemode": false,"fee": 0,"remarks": "12321","status": "unpaid","tax_amount": null,"taxes": [],"checkout_url": "https://pm.link/------","reference_number": "sadXlwd","created_at": 1670820915,"updated_at": 1670820915,"payments": []}}}

2. RestSharp调用代码

private void GenerateLink(double amount, string description, string remarks)
{
    var client = new RestClient("https://api.paymongo.com/v1/links");
    var request = new RestRequest("", Method.Post);
    request.AddHeader("accept", "application/json");
    request.AddHeader("content-type", "application/json");
    request.AddHeader("authorization", "Basic c2tfdGVzdF9aMXdma2tUaDdaUUNjR25oNnlOYUpQZks6c2tfdGVzdF9aMXdma2tUaDdaUUNjR25oNnlOYUpQZks=");
    request.AddParameter("application/json", "{\"data\":{\"attributes\":{\"amount\":" + amount + ",\"description\":\"" + description + "\",\"remarks\":\"" + remarks + "\"}}}", ParameterType.RequestBody);
    RestResponse response = client.Execute(request);
    Console.WriteLine(response.StatusCode);

    if(response.IsSuccessStatusCode)
    {
            
    }
}

private void BtnPay_Click(object sender, EventArgs e)
{
    amount = 20000;
    description = "JC DIAZ";
    remarks = "Delivery Date : " + dateTimePicker1.Value.ToString("MMM dd, yyyy");

    GenerateLink(amount, description, remarks);
}

3. 预定义的实体类

public partial class Attributes
{
   [JsonProperty("amount")]
   public int Amount = 0;

   [JsonProperty("archived")]
   public bool Archived = false;

   [JsonProperty("currency")]
   public string Currency = "";

   [JsonProperty("description")]
   public string Description = "";

   [JsonProperty("livemode")]
   public bool Livemode = false;

   [JsonProperty("fee")]
   public int Fee = 0;

   [JsonProperty("remarks")]
   public string Remarks = "";

   [JsonProperty("status")]
   public string Status = "";

   [JsonProperty("tax_amount")]
   public object TaxAmount = "";

   [JsonProperty("taxes")]
   public List<object> Taxes = null;

   [JsonProperty("checkout_url")]
   public string CheckoutUrl = "";

   [JsonProperty("reference_number")]
   public string ReferenceNumber = "";

   [JsonProperty("created_at")]
   public int CreatedAt = 0;

   [JsonProperty("updated_at")]
   public int UpdatedAt = 0;

   [JsonProperty("payments")]
   public List<object> Payments = null;
}

public partial class Data
{
   [JsonProperty("id")]
   public string Id = "";

   [JsonProperty("type")]
   public string Type = "";

   [JsonProperty("attributes")]
   public Attributes Attributes = null;
}

public partial class Model
{
   [JsonProperty("data")]
   public Data Data = null;
}

4. 尝试的代码(无结果)

var model = JsonConvert.DeserializeObject<Attributes>(response.Content);
string value = model.CheckoutUrl;

解决方案

直接反序列化为Attributes类无法成功,因为API响应的根结构是Model类(包含data节点,data节点下才是attributes)。需要先反序列化为Model,再从中提取Attributes实例。

修改GenerateLink方法中if(response.IsSuccessStatusCode)的代码块:

if(response.IsSuccessStatusCode)
{
    // 反序列化为最外层的Model类
    var model = JsonConvert.DeserializeObject<Model>(response.Content);
    // 获取Attributes实例
    var attributes = model.Data.Attributes;
    // 访问Attributes的属性,例如获取checkout_url
    string checkoutUrl = attributes.CheckoutUrl;
    Console.WriteLine(checkoutUrl);
}

注意事项

  • 确保已安装Newtonsoft.Json NuGet包(实体类中使用了JsonProperty特性,依赖该包);
  • 实体类的字段均为public,且JsonProperty特性指定的名称与API响应中的键完全匹配,无需额外调整。

内容的提问来源于stack exchange,提问作者John Carlo Diaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 17:15:38