asp.net-coreasp.net-core-6.0alpha-vantage

How to Parse Alphavantage API response to the Class in C#


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.

https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=RELIANCE.BSE&outputsize=full&apikey=demo

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.


Solution

  • I am using below code and it works for me.

    Here is the test result.

    enter image description here

        [HttpGet("test")]
        public async Task<IActionResult> testAsync()
        {
            var httpClient = _httpClientFactory.CreateClient();
            var response = await httpClient.GetAsync("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=RELIANCE.BSE&outputsize=full&apikey=demo");
            var json = await response.Content.ReadAsStringAsync();
    
            var data = JsonConvert.DeserializeObject<AlphaVantageData>(json);
    
            return Ok(data);
        }
    

    My Data class

    using Newtonsoft.Json;
    
    namespace WebApplication1.Models
    {
        public class aaa
        {
        }
        public class AlphaVantageData
        {
            [JsonProperty("Meta Data")]
            public MetaData MetaData { get; set; }
            [JsonProperty("Time Series (Daily)")]
            public Dictionary<string, TimeSeriesEntry> TimeSeries { get; set; }
        }
    
        public 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; }
        }
    
        public class TimeSeriesEntry
        {
            [JsonProperty("1. open")]
            public string Open { get; set; }
            [JsonProperty("2. high")]
            public string High { get; set; }
            [JsonProperty("3. low")]
            public string Low { get; set; }
            [JsonProperty("4. close")]
            public string Close { get; set; }
            [JsonProperty("5. adjusted close")]
            public string AdjustedClose { get; set; }
            [JsonProperty("6. volume")]
            public string Volume { get; set; }
            [JsonProperty("7. dividend amount")]
            public string DividendAmount { get; set; }
            [JsonProperty("8. split coefficient")]
            public string SplitCoefficient { get; set; }
        }
    
    }