C#中反序列化含不可预测日期键的股票数据JSON响应
问题描述
我下载了如下格式的股票定价数据:
{ "Meta Data": { "1. Information": "Intraday (5min) open, high, low, close prices and volume", "2. Symbol": "IBM", "3. Last Refreshed": "2023-08-01 19:55:00", "4. Interval": "5min", "5. Output Size": "Compact", "6. Time Zone": "US/Eastern" }, "Time Series (5min)": { "2023-08-01 19:55:00": { "1. open": "142.8100", "2. high": "143.1000", "3. low": "142.8100", "4. close": "142.8100", "5. volume": "230" }, "2023-08-01 19:45:00": { "1. open": "143.0000", "2. high": "143.0000", "3. low": "143.0000", "4. close": "143.0000", "5. volume": "50" } } }
我希望在C#中对其进行反序列化,Meta Data部分结构固定,已能正确反序列化,但"Time Series (5min)"下的日期是动态不可预知的,每个日期对应一组价格字段,无法用固定JsonProperty匹配日期键。现有尝试代码如下:
public class json_price { [JsonProperty("Meta Data")] public headers Headers { get; set; } public class headers { [JsonProperty("1. Information")] public string s1 { get; set; } [JsonProperty("2. Symbol")] public string s2 { get; set; } [JsonProperty("3. Last Refreshed")] public string s3 { get; set; } [JsonProperty("4. Interval")] public string s4 { get; set; } [JsonProperty("5. Output Size")] public string s5 { get; set; } [JsonProperty("6. Time Zone")] public string s6 { get; set; } } [JsonProperty("Time Series 5min")] public priceheader ph { get; set; } public class priceheader { public datepricing[] dp { get; set; } } public class datepricing { [JsonProperty("1. open")] public long O { get; set; } [JsonProperty("2. high")] public long H { get; set; } [JsonProperty("3. low")] public long L { get; set; } [JsonProperty("4. close")] public long C { get; set; } [JsonProperty("5. volume")] public long V { get; set; } } }
使用JsonConvert.DeserializeObject<json_price>(output)反序列化时,Headers对象能正确填充,但ph对象为空,求解决方法。
解决方案
你的问题核心是JsonProperty名称不匹配和动态键处理方式错误,以下是具体修正方案:
1. 核心问题修正点
- 匹配正确的JsonProperty名称:JSON中对应的键是
"Time Series (5min)",你代码里写成了"Time Series 5min",缺少括号导致无法映射。 - 用Dictionary处理动态日期键:对于动态的字符串日期键,直接使用
Dictionary<string, 数据类型>,Json.NET会自动将每个日期键映射为字典的Key,对应的值反序列化为指定的价格对象,无需提前知道具体日期。 - 修正字段类型:价格是带小数的数值,不能用
long(整数类型),改用decimal更适合金融数据;成交量是整数,可保留long。
2. 完整修正代码
using Newtonsoft.Json; using System.Collections.Generic; public class StockPriceData { [JsonProperty("Meta Data")] public MetaData MetaData { get; set; } [JsonProperty("Time Series (5min)")] public Dictionary<string, PricingEntry> TimeSeries { get; set; } public class MetaData { [JsonProperty("1. Information")] public string Information { get; set; } [JsonProperty("2. Symbol")] public string Symbol { get; set; } [JsonProperty("3. Last Refreshed")] public string LastRefreshed { get; set; } [JsonProperty("4. Interval")] public string Interval { get; set; } [JsonProperty("5. Output Size")] public string OutputSize { get; set; } [JsonProperty("6. Time Zone")] public string TimeZone { get; set; } } public class PricingEntry { [JsonProperty("1. open")] public decimal Open { get; set; } [JsonProperty("2. high")] public decimal High { get; set; } [JsonProperty("3. low")] public decimal Low { get; set; } [JsonProperty("4. close")] public decimal Close { get; set; } [JsonProperty("5. volume")] public long Volume { get; set; } } }
3. 反序列化及使用示例
// 假设output是你的JSON字符串 var stockData = JsonConvert.DeserializeObject<StockPriceData>(output); // 遍历动态的时间序列数据 foreach (var entry in stockData.TimeSeries) { string timestamp = entry.Key; PricingEntry pricing = entry.Value; // 按需处理数据,比如打印 System.Console.WriteLine($"时间:{timestamp},开盘价:{pricing.Open},收盘价:{pricing.Close}"); }
原理说明
Json.NET对Dictionary<string, T>类型有原生支持,会自动遍历JSON对象中的所有键值对,将键作为字典的Key,值反序列化为T类型的对象,完美适配这种动态键的场景。
内容的提问来源于stack exchange,提问作者DanB
相关产品推荐
相关产品推荐

