'Async task getting value off of JSON return, XAML/WPF app
I am trying to get a single value off of a JSON return.
My call is doing an HttpClient call to a webservice, it returns me my Json object. I need to now get a value off of the Json return in my class so I can pass it to another method in my class as a parameter.
Current code:
var url = "https://localhost:9999/create_payment_intent";
var terminalAmount = new PaymentIntentCreateRequest();
terminalAmount.Amount = "2300";
var json = JsonConvert.SerializeObject(terminalAmount);
var data = new StringContent(json, Encoding.UTF8, "application/json");
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.PostAsync(url, data); //The resulting response
if (response.IsSuccessStatusCode)
{
var paymentIntentResponse = response.Content.ReadAsStringAsync().Result;
var jsonObject = JsonConvert.DeserializeObject<object>(paymentIntentResponse.ToString());
//jsonObject returns some values {"id": "pi_xxxxxxxxx", some other data.....}
// PaymentIntentId = jsonObject.Id; // I NEED THE ID VALUE OFF OF THE JSON OBJECT
//PaymentIntentId = result.Id;
//Call next function if result has an ID if not jump out and try again or return error
}
//Successful payment intent ready?
//pass the payment intent amount to the variable
//await response;
Console.WriteLine(response);
}
Solution 1:[1]
At this line you need to deserialize to an object which has the fields for what you expect to get back from the call
var jsonObject = JsonConvert.DeserializeObject<PaymentResponseObject>(paymentIntentResponse.ToString());
example being
public class PaymentResponseObject{
public string Id {get;set;}
// all other fields
}
You can then grab the fields you want from it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gulfaran Younis |
