I am building an MVC app and I am pulling data from an API. However, when I run the app, I get the error below. If I remove the line of code causing the error, then the app runs fine but no data is returned.
Below is the link to the error message screenshot
public List<DailyEquity> GetDailyChart(string symbol)
{
// string to specify information to be retrieved from the API
string IEXTrading_API_PATH = BASE_URL + "stock/" + symbol + "/chart/1d";
// initialize objects needed to gather data
string Dailycharts = "";
List<DailyEquity> DailyEquities = new List<DailyEquity>();
httpClient.BaseAddress = new Uri(IEXTrading_API_PATH);
// connect to the API and obtain the response
HttpResponseMessage response = httpClient.GetAsync(IEXTrading_API_PATH).GetAwaiter().GetResult();
// now, obtain the Json objects in the response as a string
if (response.IsSuccessStatusCode)
{
Dailycharts = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
var aa = Dailycharts.GetType();
}
// parse the string into appropriate objects
if (!Dailycharts.Equals(""))
{
DailyChartRoot dailyroot = JsonConvert.DeserializeObject<DailyChartRoot>(Dailycharts,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
DailyEquities = dailyroot.dailychart.ToList();
}
// fix the relations. By default the quotes do not have the company symbol
// this symbol serves as the foreign key in the database and connects the quote to the company
foreach (DailyEquity Equity in DailyEquities)
{
Equity.symbol = symbol;
}
return DailyEquities;
}
But an error is thrown at:
// parse the string into appropriate objects
if (!Dailycharts.Equals(""))
{
DailyChartRoot dailyroot = JsonConvert.DeserializeObject<DailyChartRoot>(Dailycharts,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
DailyEquities = dailyroot.dailychart.ToList();
}
"Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'API_Usage.Models.DailyChartRoot' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.'"
This is the Model class from the Model folder.
public class DailyChartRoot
{
public DailyEquity[] dailychart { get; set; }
}
public class DailyEquity
{
public int DailyEquityId { get; set; }
public string minute { get; set; }
public float marketaverage { get; set; }
public float marketnotional { get; set; }
public float marketnumberoftrades { get; set; }
public float marketopen { get; set; }
public float marketclose { get; set; }
public float markethigh { get; set; }
public float marketlow { get; set; }
public float marketvolume { get; set; }
public float average { get; set; }
public string symbol { get; set; }
}
This is a sample output from the API:
[{"date":"20190405","minute":"09:30","label":"09:30 AM","high":196.47,"low":196.14,"average":196.271,"volume":2360,"notional":463200.325,"numberOfTrades":26,"marketHigh":196.48,"marketLow":196.14,"marketAverage":196.354,"marketVolume":308339,"marketNotional":60543617.1182,"marketNumberOfTrades":712,"open":196.47,"close":196.21,"marketOpen":196.45,"marketClose":196.217,"changeOverTime":0,"marketChangeOverTime":0},{"date":"20190405","minute":"09:31","label":"09:31 AM","high":196.28,"low":196.07,"average":196.147,"volume":2148,"notional":421324.74,"numberOfTrades":25,"marketHigh":196.289,"marketLow":195.93,"marketAverage":196.113,"marketVolume":210621,"marketNotional":41305476.8289,"marketNumberOfTrades":1018,"open":196.1,"close":196.26,"marketOpen":196.22,"marketClose":196.24,"changeOverTime":-0.0006317795293242263,"marketChangeOverTime":-0.0012273750471088639}