将JSON结果存储到数组excel vba

时间:2019-07-19 14:07:28

标签: json excel vba

我尝试使用API​​来获取有关Yahoo Finance的一些信息 这是我创建的UDF

Sub Test()
'1 >> High & 2 >> Close
MsgBox YahooHigh("GOOG", "2019-07-18", 1)
MsgBox YahooHigh("GOOG", "2019-07-18", 2)
End Sub

Function YahooHigh(sTicker As String, sDate As String, idx As Integer)
Dim json As Object

With CreateObject("WinHttp.WinHttpRequest.5.1")
    .Open "GET", "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=" & sTicker & "&outputsize=full&apikey=myapikey"
    .Send
    Set json = JsonConverter.ParseJson(.responseText)
End With

If idx = 1 Then
    YahooHigh = json("Time Series (Daily)")(sDate)("2. high")
ElseIf idx = 2 Then
    YahooHigh = json("Time Series (Daily)")(sDate)("4. close")
Else
    YahooHigh = Empty
End If
End Function

UDF可以正常工作,但是当然我每次都必须加载JSON结果。在我的示例中,UDF将为高值运行两次,对于高值运行第二次 有没有一种方法可以将json结果存储到数组中,然后调用该数组而不是加载json。我想到了静态的,但我对此一无所知

我想做的是将所有日期存储在特定结果的json结果中(仅限高值和收盘价),然后从静态数组中调用所需的值..有什么想法吗?

另一种变化: 我尝试使用HTML内容,当直接使用链接时,它对我来说很好用

Sub MyTest()
Dim html As Object, ele As Object

With CreateObject("MSXML2.ServerXMLHTTP")
                 'https://finance.yahoo.com/quote/GOOG/history?period1=1325566800&period2=1325566800&interval=1d&filter=history&frequency=1d
    '.Open "GET", "https://finance.yahoo.com/quote/GOOG/history?period1=1325566800&period2=1325566800&interval=1d&filter=history&frequency=1d", False
    Dim sTicker As String
    sTicker = Sheets(1).Range("B1").Value   'GOOG

    Dim period1 As Long, period2 As Long
    period1 = ToUnix(Sheets(1).Range("B2").Value) '3 Jan 2012
    period2 = ToUnix(Sheets(1).Range("B3").Value) '3 Jan 2012

    .Open "GET", "https://finance.yahoo.com/quote/" & sTicker & "/history?period1=" & period1 & "&period2=" & period2 & "&interval=1d&filter=history&frequency=1d", False
    .Send

    If .Status <> 200 Then MsgBox "Problem" & vbNewLine & .Status & " - " & .StatusText: Exit Sub

    Set html = CreateObject("htmlfile")
    html.body.innerHTML = .responseText

    'WriteTxtFile html.body.innerHTML

    'Stop

    Set ele = html.getElementsByTagName("table")(0).getElementsByTagName("tr")(1)

    Dim tCell As Object
    Dim cnt As Long

    For Each tCell In ele.Children
        cnt = cnt + 1

        If cnt = 3 Then Debug.Print "High: " & tCell.innerText
        If cnt = 5 Then Debug.Print "Close: " & tCell.innerText
    Next tCell
End With
End Sub

Public Function ToUnix(dt) As Long
ToUnix = DateDiff("s", "1/1/1970", dt)
End Function

使用此行.Open "GET", "https://finance.yahoo.com/quote/GOOG/history?period1=1325566800&period2=1325566800&interval=1d&filter=history&frequency=1d", False时,它可以正常工作并从“高”和“关闭”返回值 但是,当尝试将日期从工作表转换为UNIX以便在链接中使用它们时,它不起作用 现在这是我的问题

1 个答案:

答案 0 :(得分:1)

只需让您的函数返回json对象,然后在您的sub中解析它即可。 json对象将包含您的所有数据,您可以解析出想要的内容。

例如

在您的职能中:

import pandas as pd

csv_file = pd.read_csv(path_to_the_file_to_read)

和您的Sub:

Function YahooHigh(sTicker As String) as object
Dim json As Object

With CreateObject("WinHttp.WinHttpRequest.5.1")
    .Open "GET", "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=" & sTicker & "&outputsize=full&apikey=myapikey"
    .Send
    Set YahooHigh = JsonConverter.ParseJson(.responseText)
End With