You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#反序列化API返回JSON时TimeSeries字段始终为null问题排查

问题分析与修复方案

嘿,我一眼就看出问题所在了——你的TimeSeries类结构和API返回的JSON完全不匹配,这就是为什么反序列化后TimeSeries一直为null的原因!

咱们先看API返回的JSON里的"Time Series FX (1min)"部分:它是一个键值对字典,键是时间戳字符串(比如"2019-04-19 09:19:00"),值是对应的行情数据对象。但你定义的TimeSeries类里用的是List<Time> Times,这是一个列表结构,序列化器根本没法把字典格式的JSON转换成列表,自然就赋值为null了。

修复方案

你有两种选择,根据你的需求来选:

方案1:直接把Data类里的TimeSeries属性改成字典类型(最简洁)

不需要额外的TimeSeries类,直接用Dictionary<string, Time>来匹配JSON结构:

public class Data
{
    [JsonProperty("Meta Data")]
    public MetaData MetaData { get; set; }
    // 把原来的TimeSeries类型换成Dictionary<string, Time>
    [JsonProperty("Time Series FX (1min)")]
    public Dictionary<string, Time> TimeSeries { get; set; }
}

// 你的MetaData和Time类保持不变
public class MetaData
{
    [JsonProperty("1. Information")]
    public string Information { get; set; }
    [JsonProperty("2. From Symbol")]
    public string FromSymbol { get; set; }
    [JsonProperty("3. To Symbol")]
    public string ToSymbol { get; set; }
    [JsonProperty("4. Last Refreshed")]
    public string LastRefreshed { get; set; }
    [JsonProperty("5. Interval")]
    public string Interval { get; set; }
    [JsonProperty("6. Output Size")]
    public string OutputSize { get; set; }
    [JsonProperty("7. Time Zone")]
    public string TimeZone { get; set; }
}

public class Time
{
    [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; }
}

方案2:保留TimeSeries类,但内部用字典存储

如果你一定要保留TimeSeries这个类(比如后续有扩展需求),可以把类里的List<Time>改成Dictionary<string, Time>:

public class Data
{
    [JsonProperty("Meta Data")]
    public MetaData MetaData { get; set; }
    [JsonProperty("Time Series FX (1min)")]
    public TimeSeries TimeSeries { get; set; }
}

public class TimeSeries
{
    // 用字典来匹配JSON的键值对结构
    public Dictionary<string, Time> TimeEntries { get; set; }
}

// MetaData和Time类保持不变

验证反序列化

用Newtonsoft.Json(你应该已经在使用它,因为用了JsonProperty特性)测试一下:

string apiJson = @"{""Meta Data"": {""1. Information"": ""FX Intraday (1min) Time Series"", ""2. From Symbol"": ""USD"", ""3. To Symbol"": ""EUR"", ""4. Last Refreshed"": ""2019-04-19 09:19:00"", ""5. Interval"": ""1min"", ""6. Output Size"": ""Compact"", ""7. Time Zone"": ""UTC"" }, ""Time Series FX (1min)"": {""2019-04-19 09:19:00"": {""1. open"": ""0.8890"", ""2. high"": ""0.8890"", ""3. low"": ""0.8890"", ""4. close"": ""0.8890"" }, ""2019-04-19 09:18:00"": {""1. open"": ""0.8890"", ""2. high"": ""0.8890"", ""3. low"": ""0.8890"", ""4. close"": ""0.8890"" } } }";

Data result = JsonConvert.DeserializeObject<Data>(apiJson);

// 现在可以正常访问TimeSeries里的数据了
foreach (var timeEntry in result.TimeSeries)
{
    Console.WriteLine($"时间: {timeEntry.Key}, 收盘价: {timeEntry.Value.Close}");
}

这样修改后,TimeSeries就不会再是null了,序列化器能正确把JSON里的字典结构映射到C#的Dictionary类型上。

内容的提问来源于stack exchange,提问作者Uday Teja

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:18:44