我得到一个来自外部API的JSON响应,并且我尝试反序列化时遇到了一些问题。这里是JSON:c#解析时间序列数据
{
"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"
}
}
}
这里是我试图反序列化为类:
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; }
}
这反序列化空。我认为TimeSeries类是正确的,但我不知道如何处理更改日期。使用json2csharp不会为我创建有效的类,它告诉我JSON无效。
感谢您的帮助。
这不是一个有效的JSON结构和TimeSeriesDaily类也错误地构成。这里没有阵列。 JSON数组就像[] – Steve
我明白这不是一个数组,但我不知道如何构建我的类来反序列化。 – kwcolson98
我想这会很困难。也许动态解析与Json.NET将是去.. https://weblog.west-wind.com/posts/2012/aug/30/using-jsonnet-for-dynamic-json-parsing – Steve