I am new to API, RestSharp and JSON. I got the Response Data from the API, my problem is I didn't know how to pass the response data to JSON Properties that I've been readied.
Here is my Code.
This is the API response I received.
{
"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": []
}
}
}
RestSharp Code:
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);
}
The variables that I want to fill with RestSharp Response Contents
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;
}
I tried this code, but it still returns no results.
var model = JsonConvert.DeserializeObject<Attributes>(response.Content);
string value = model.CheckoutUrl;
I expect to populate the attributes class with the contents of RestResponse.
you will have to create a root class and use it to deserialize your json
Model model = JsonConvert.DeserializeObject<Model>(response.Content);
string value = model.Data.Attributes.CheckoutUrl;
public partial class Model
{
[JsonProperty("data")]
public Data data { get; set; }
}
public partial class Data
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("attributes")]
public Attributes Attributes { get; set; }
}
or if you only need attributes data, you can do it without the extra classes
Attributes attributes = JObject.Parse(response.Content)["Data"]["Attributes"].ToObject<Attributes>();
string checkoutUrl = attributes.CheckoutUrl;