I have the following JSON:
{
"TimeSeries": {
"2019-04-26": {
"open": "20.9000",
"high": "21.0000",
"low": "20.7300",
"close": "20.7300",
"volume": "556200"
},
"2019-04-25": {
"open": "20.8000",
"high": "20.9100",
"low": "20.6600",
"close": "20.7800",
"volume": "784200"
}
}
}
I Need Deserialize to a C# Object. The problem is the Column format, that represents the Date, is dynamic.
I've tried with Newtonsoft JSON, but to no avail. How do I turn this JSON into this object?
public class PriceHistory
{
public decimal Open { get; set; }
public decimal High { get; set; }
public decimal Low { get; set; }
public decimal Close { get; set; }
public decimal Volume { get; set; }
//THE PROBLEM IS THIS FIELD
public DateTime Date { get; set; }
}
Vanderlan GomesVanderlan Gomes
asked Apr 27, 2019 at 13:44
-
How you Deserialize?Prashant Pimpale– Prashant Pimpale2019年04月27日 13:48:18 +00:00Commented Apr 27, 2019 at 13:48
-
The question is precisely this, I do not know how to deserialize :/Vanderlan Gomes– Vanderlan Gomes2019年04月27日 13:54:26 +00:00Commented Apr 27, 2019 at 13:54
1 Answer 1
Use these classes:
public class Root
{
public Dictionary<DateTime, PriceHistory> TimeSeries { get; set; }
}
public class PriceHistory
{
public decimal Open { get; set; }
public decimal High { get; set; }
public decimal Low { get; set; }
public decimal Close { get; set; }
public decimal Volume { get; set; }
}
Deserialization:
var json = File.ReadAllText("test.json");
var root = JsonConvert.DeserializeObject<Root>(json);
answered Apr 27, 2019 at 14:08
Comments
lang-cs