无法从Yahoo财务库存数据解析JSON字符串到CSV

时间:2017-05-22 10:30:48

标签: c# .net json csv yahoo-finance

我尝试使用互联网上提供的各种工具(Pushing read-only GUI properties back into ViewModel等)将雅虎财务股票数据中的JSON字符串从以下网址解析为CSV。

JSON.NET

我想将JSON字符串解析为CSV,格式如下。

日期|打开|高|低|关闭|体积

请任何人帮我解决这个问题。提前谢谢。

1 个答案:

答案 0 :(得分:1)

1)您需要为Yahoo JSON模式定义类:(已修改:某些值可能为null。因此,我将它们修改为无效变量)

public class Pre
{
    public string timezone { get; set; }
    public int end { get; set; }
    public int start { get; set; }
    public int gmtoffset { get; set; }
}

public class Regular
{
    public string timezone { get; set; }
    public int end { get; set; }
    public int start { get; set; }
    public int gmtoffset { get; set; }
}

public class Post
{
    public string timezone { get; set; }
    public int end { get; set; }
    public int start { get; set; }
    public int gmtoffset { get; set; }
}

public class CurrentTradingPeriod
{
    public Pre pre { get; set; }
    public Regular regular { get; set; }
    public Post post { get; set; }
}

public class Meta
{
    public string currency { get; set; }
    public string symbol { get; set; }
    public string exchangeName { get; set; }
    public string instrumentType { get; set; }
    public int firstTradeDate { get; set; }
    public int gmtoffset { get; set; }
    public string timezone { get; set; }
    public string exchangeTimezoneName { get; set; }
    public CurrentTradingPeriod currentTradingPeriod { get; set; }
    public string dataGranularity { get; set; }
    public List<string> validRanges { get; set; }
}

public class Quote
{
    public List<object> volume { get; set; }
    public List<double?> low { get; set; }
    public List<double?> high { get; set; }
    public List<double?> close { get; set; }
    public List<double?> open { get; set; }
}

public class Unadjclose
{
    public List<double?> unadjclose { get; set; }
}

public class Unadjquote
{
    public List<double?> unadjopen { get; set; }
    public List<double?> unadjclose { get; set; }
    public List<double?> unadjhigh { get; set; }
    public List<double?> unadjlow { get; set; }
}

public class Indicators
{
    public List<Quote> quote { get; set; }
    public List<Unadjclose> unadjclose { get; set; }
    public List<Unadjquote> unadjquote { get; set; }
}

public class Result
{
    public Meta meta { get; set; }
    public List<int> timestamp { get; set; }
    public Indicators indicators { get; set; }
}

public class Chart
{
    public List<Result> result { get; set; }
    public object error { get; set; }
}

public class RootObject
{
    public Chart chart { get; set; }
}

2)您需要将JSON反序列化为对象

var str = wc.DownloadString("https://query1.finance.yahoo.com/v7/finance/chart/MSFT?range=25y&interval=1d&indicators=quote&includeTimestamps=true&includePrePost=false&corsDomain=finance.yahoo.com");
var data = JsonConvert.DeserializeObject<Rootobject>(str);

3)然后,遍历此对象并构建CSV。工作示例:(已修改:某些值可能为null。因此,我已修改代码以检查在转换为字符串之前,使用HasValue属性保留值为null的变量值是否存在?

var wc = new WebClient();
var str = wc.DownloadString("https://query1.finance.yahoo.com/v7/finance/chart/MSFT?range=25y&interval=1d&indicators=quote&includeTimestamps=true&includePrePost=false&corsDomain=finance.yahoo.com");
var data = JsonConvert.DeserializeObject<Rootobject>(str);
var result = new List<string>();
var quotesInfo = data.chart.result.First();
for (var i = 0; i < quotesInfo.timestamp.Count; i++)
{
    var quotesStr = new List<string>();
    var quoteData = quotesInfo.indicators.quote.First();
    quotesStr.Add(UnixTimeStampToDateTime(quotesInfo.timestamp[i]).ToString(CultureInfo.InvariantCulture));
    quotesStr.Add(quoteData.open[i].HasValue ? quoteData.open[i].ToString() : string.Empty);
    quotesStr.Add(quoteData.high[i].HasValue ? quoteData.high[i].ToString() : string.Empty);
    quotesStr.Add(quoteData.low[i].HasValue ? quoteData.low[i].ToString() : string.Empty);
    quotesStr.Add(quoteData.close[i].HasValue ? quoteData.close[i].ToString() : string.Empty);
    quotesStr.Add(quoteData.volume[i] != null ? quoteData.volume[i].ToString() : string.Empty);
    result.Add(string.Join(",", quotesStr));
}
File.WriteAllLines("result.csv",result);

已修改:并且,我已添加了将timestamp转换为DateTime格式的代码。

public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
{
    var dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
    dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToUniversalTime();
    return dtDateTime;
}

最后,您将收到逗号分隔的CSV文件