2017-06-02 63 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; } 

}