我得到一個來自外部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