2017-06-02 59 views
1

我得到一个来自外部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无效。

感谢您的帮助。

+2

这不是一个有效的JSON结构和TimeSeriesDaily类也错误地构成。这里没有阵列。 JSON数组就像[] – Steve

+0

我明白这不是一个数组,但我不知道如何构建我的类来反序列化。 – kwcolson98

+0

我想这会很困难。也许动态解析与Json.NET将是去.. https://weblog.west-wind.com/posts/2012/aug/30/using-jsonnet-for-dynamic-json-parsing – Steve

回答

0

你错过了JSON中的最后一个卷曲,我想。

+0

添加结束大括号 – kwcolson98

0

请注意,您使用的是非标准名称作为对象。例如,连字符在C#变量中是不允许的。变量也不能以数字开头。

生成的类应该看起来像这样。

using Newtonsoft.Json; 
using Newtonsoft.Json.Linq; 

using System; 
using System.Collections.Generic; 

namespace kwcolson98 
{ 
    public class StockQuote 
    { 
     [JsonProperty("Time Series (Daily)")] 
     public TimeSeriesDaily _TimeSeriesDaily { get; set; } 


     public class TimeSeriesDaily 
     { 
      [JsonProperty("2017-06-01")] 
      public TimeSeries _20170601 { get; set; } 

      [JsonProperty("2017-05-31")] 
      public TimeSeries _20170531 { 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; } 
      } 
     } 
    } 
} 
+0

虽然这是正确的,但我认为实际的JSON日期会随着时间的推移而改变,所以只会使其对于该实例有效。 – Steve

+1

然后,设计JSON的正确方法是使用数组,而不是:) – silkfire

1

我工作了同样的问题,并想后我的解决方案。我使用了部分代码,并按如下所示完成了它。我认为它可行,但我只是刚刚开始研究这个。

class CustomDateTimeConverter : IsoDateTimeConverter 
{ 
    public CustomDateTimeConverter() 
    { 
     base.DateTimeFormat = "yyyy-mm-dd"; 
    } 
} 


public class StockQuote 
{ 
    [JsonProperty("Time Series (Daily)")] 
    public Dictionary<string, TimeSeries> tsd { get; set; } 
} 


public class TimeSeriesDaily 
{ 
    [JsonProperty(ItemConverterType = typeof(CustomDateTimeConverter))] 
    public TimeSeries ts { 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; } 

}