C#解析时间序列数据 [英] c# parsing time series data

查看:425
本文介绍了C#解析时间序列数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从外部API收到JSON响应,尝试反序列化时遇到一些问题.这是JSON:

I am getting a JSON response from an external API and I am having a bit of a problem trying to deserialize. Here is the 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"
    }
}
}

以下是我尝试反序列化的类:

Here are the classes that I tried to deserialize into:

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无效.

This deserializes as null. I think that the class TimeSeries is correct, but I am not sure how to handle the changing date. Using json2csharp does not create valid classes for me, it tells me the JSON is invalid.

感谢您的帮助.

推荐答案

我正在处理相同的问题,并希望发布我的解决方案.我使用了部分代码,并按如下所示完成了代码.我认为它可行,但是我才刚刚开始进行这项工作.

I am working on the same problem and wanted to post my solution. I used part of your code and completed it as follows. I think it works but I have only just started working on this.

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

}

这篇关于C#解析时间序列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆