I am getting a JSON response from an external API and I am having a bit of a problem trying to deserialize. Here is the JSON:
{
"Time Series (Daily)": {
"2017-06-01": {
"1. open": "70.2400",
"2. high": "70.6100",
"3. low": "69.4510",
"4. close": "70.1000",
"5. volume": "21066468"
},
"2017-05-31": {
"1. open": "70.5300",
"2. high": "70.7400",
"3. low": "69.8100",
"4. close": "69.8400",
"5. volume": "30436364"
}
}
}
Here are the classes that I tried to deserialize into:
public class StockQuote
{
[JsonProperty("Time Series (Daily)")]
public TimeSeriesDaily Daily { get; set; }
}
public class TimeSeriesDaily
{
public string Date { get; set; }
public TimeSeries[] Daily { get; set; }
}
public class TimeSeries
{
[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. volume")]
public string Volume { get; set; }
}
This deserializes as null. I think that the class TimeSeries is correct, but I am not sure how to handle the changing date. Using json2csharp does not create valid classes for me, it tells me the JSON is invalid.
Thanks for your help.
I am working on the same problem and wanted to post my solution. I used part of your code and completed it as follows. I think it works but I have only just started working on this.
class CustomDateTimeConverter : IsoDateTimeConverter
{
public CustomDateTimeConverter()
{
base.DateTimeFormat = "yyyy-mm-dd";
}
}
public class StockQuote
{
[JsonProperty("Time Series (Daily)")]
public Dictionary<string, TimeSeries> tsd { get; set; }
}
public class TimeSeriesDaily
{
[JsonProperty(ItemConverterType = typeof(CustomDateTimeConverter))]
public TimeSeries ts { get; set; }
}
public class TimeSeries
{
[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. volume")]
public string Volume { get; set; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With