I am using Alphavantage API to retrieve Stock data. I am able to get data, but data is not easy to bind into class. Below is the data I am getting from API.
This is valid JSON, but I don't know how to create a class which can bind this JSON data.
Below is the CLASS I tried with, but no luck,
class AlphaVantageData
{
[JsonProperty("Meta Data")]
public MetaData MetaData { get; set; }
public TimeSeries TimeSeries { get; set; }
}
class MetaData
{
[JsonProperty("1. Information")]
public string Information { get; set; }
[JsonProperty("2. Symbol")]
public string Symbol { get; set; }
[JsonProperty("3. Last Refreshed")]
public string LastRefreshed { get; set; }
[JsonProperty("4. Output Size")]
public string OutputSize { get; set; }
[JsonProperty("5. Time Zone")]
public string TimeZone { get; set; }
}
class TimeSeries
{
public Dictionary<string, string> Close { get; set; }
}
I am using .NET core 6 framework.
Below is the sample code,
var client = new HttpClient();
var request = client.GetAsync("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=RELIANCE.BSE&outputsize=full&apikey=demo");
var response = await request;
var json = response.Content.ReadAsStringAsync().Result;
var data = JsonSerializer.Deserialize<AlphaVantageData>(json);
Data property have 2 objects, but both are null.
I am using below code and it works for me.
Here is the test result.
My Data class