I'm trying to deserialize use the following Json and classes:
List<Root> OBJTEST = (List<Root>)Newtonsoft.Json.JsonConvert.DeserializeObject(response, typeof(List<Root>));
You don't have an array value there. You should deserialize to object Root obj = Newtonsoft.Json.JsonConvert.DeserializeObject<Root>(response)<\/code> .
{
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "IBM",
"3. Last Refreshed": "2022-02-04",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2022-02-04": {
"1. open": "137.8600",
"2. high": "138.8200",
"3. low": "136.2150",
"4. close": "137.1500",
"5. volume": "4142045"
},
"2022-02-03": {
"1. open": "137.0000",
"2. high": "138.7600",
"3. low": "135.8310",
"4. close": "137.7800",
"5. volume": "6100777"
}
}
}
try this
string json = string.Empty;
using (var client = new HttpClient())
{
var baseAddress = "https://www.alphavantage.co";
var api = "/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=demo";
client.BaseAddress = new Uri(baseAddress);
var contentType = new MediaTypeWithQualityHeaderValue("application/json");
client.DefaultRequestHeaders.Accept.Add(contentType);
var response = client.GetAsync(api).Result;
if (response.IsSuccessStatusCode)
json = response.Content.ReadAsStringAsync().Result;
}
Data data =JsonConvert.DeserializeObject<Data>(json);
and you can get TimeSeriesDaily list
List<TimeSeriesDaily> timeSeriesDaily = data.TimeSeriesDaily.Select(tsd => AddDate(tsd) ).ToList();
private TimeSeriesDaily AddDate(KeyValuePair<string, TimeSeriesDaily> tsdd)
{
var tsd=tsdd.Value;
tsd.Date=tsdd.Key;
return tsd;
}
classes
public partial class Data
{
[JsonProperty("Meta Data")]
public MetaData MetaData { get; set; }
[JsonProperty("Time Series (Daily)")]
public Dictionary<string, TimeSeriesDaily> TimeSeriesDaily { get; set; }
}
public partial class MetaData
{
[JsonProperty("1. Information")]
public string The1Information { get; set; }
[JsonProperty("2. Symbol")]
public string The2Symbol { get; set; }
[JsonProperty("3. Last Refreshed")]
public DateTimeOffset The3LastRefreshed { get; set; }
[JsonProperty("4. Output Size")]
public string The4OutputSize { get; set; }
[JsonProperty("5. Time Zone")]
public string The5TimeZone { get; set; }
}
public partial class TimeSeriesDaily
{
[JsonIgnore]
public string Date { get; set; }
[JsonProperty("1. open")]
public string The1Open { get; set; }
[JsonProperty("2. high")]
public string The2High { get; set; }
[JsonProperty("3. low")]
public string The3Low { get; set; }
[JsonProperty("4. close")]
public string The4Close { get; set; }
[JsonProperty("5. volume")]
public long The5Volume { get; set; }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.