I need only the "points": 300 from the below response body.
Here is the body:
{
"channel": "abcd",
"username": "fgh",
"points": 300,
"pointsAlltime": 0,
"watchtime": 0,
"rank": 1
}
The Code:
var client = new HttpClient();
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("https://api.streamelements.com/kappa/v2/points/abcd/fgh"),
Headers =
{
{ "Accept", "application/json" },
{ "Authorization", "Bearer 123" },
},
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
Console.WriteLine(body);
}
I need only the points value. – 300
>Solution :
You need to deserialize your string response into JSON and with the help of the property name you need to extract the points value.
Using JObject.Parse(String),
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadAsStringAsync();
JObject jObj = JObject.Parse(result);
Console.WriteLine(jObj["points"]);
}
You can also try the native .NET approach introduced in .NET 6+ versions i.e. JsonNode.Parse(),
var jNode = JsonNode.Parse(result);
Console.WriteLine(jNode["points"]);