【问题标题】:c# parsing time series datac#解析时间序列数据
【发布时间】:2017-06-02 11:06:50
【问题描述】:

我从外部 API 获得 JSON 响应,但在尝试反序列化时遇到了一点问题。这是 JSON:

{
"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; }
}

这将反序列化为 null。我认为 TimeSeries 类是正确的,但我不确定如何处理更改日期。使用 json2csharp 不会为我创建有效的类,它告诉我 JSON 无效。

感谢您的帮助。

【问题讨论】:

  • 这不是一个有效的 JSON 结构,而且 TimeSeriesDaily 类的结构也不正确。这里没有数组。 JSON 中的数组类似于 [ ]
  • 我知道这不是一个数组,但我不确定如何构造我的类来反序列化。
  • 我认为这将非常困难。也许使用 Json.NET 进行动态解析是可行的方法。weblog.west-wind.com/posts/2012/aug/30/…

标签: c# json json-deserialization


【解决方案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; }

}

【讨论】:

    【解决方案2】:

    我认为您在 JSON 中缺少最后一个右花键。

    【讨论】:

    • 添加了右花括号
    【解决方案3】:

    请注意,您正在为对象使用非标准名称。例如,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; }
                }
            }
        }
    }
    

    【讨论】:

    • 虽然这是正确的,但我认为实际的 JSON 日期会随着时间的推移而改变,因此只会使其对该实例有效。
    • 那么设计JSON的正确方法是使用数组代替:)
    猜你喜欢
    • 2015-04-11
    • 2016-11-15
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 2021-05-31
    • 2016-08-15
    相关资源
    最近更新 更多