【问题标题】:Unable to parse JSON string to CSV from Yahoo finance stock data无法将 JSON 字符串从 Yahoo Finance 股票数据解析为 CSV
【发布时间】:2017-05-22 10:30:47
【问题描述】:

我尝试使用互联网上提供的各种工具(JSON.NET 等)将来自 Yahoo Finance 股票数据的 JSON 字符串从以下 URL 解析为 CSV。

MSFT Yahoo Data

我想将 JSON 字符串解析为以下格式的 CSV。

日期 |打开 |高 |低 |关闭 |音量

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

【问题讨论】:

  • 你需要什么格式的CSV?
  • @VictorLeontyev 请查看编辑后的问题

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


【解决方案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 属性在转换为字符串之前保留值)

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 文件

【讨论】:

  • 感谢您的宝贵回复。是否可以在 VisualBasic 6.0 中做同样的事情?
  • 以上链接会将其转换为 VB.Net。我需要的是 VB6
  • 那我不确定。您用 C# 标记的问题,我在 C# 中提供了答案
猜你喜欢
  • 1970-01-01
  • 2011-01-17
  • 1970-01-01
  • 1970-01-01
  • 2018-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多