VBA - API 呼叫显示在 Excel

VBA - API call displayed in Excel

我试图在 Excel sheet 中显示特定加密货币的价格。我正在从 CoinMarketCap 的 API 中提取 JSON 数据 - https://api.coinmarketcap.com/v1/ticker/

最终,我试图获取 Ripple 的价格(第 16 行),然后在我的 Excel sheet 中设置单元格 B1 以显示 Ripple 的价格(第 17 行)。

这是我的脚本,但由于某种原因无法正常工作。

Sub test()

Dim httpObject As Object
Set httpObject = CreateObject("MSXML2.XMLHTTP")

sURL = "https://api.coinmarketcap.com/v1/ticker/"

sRequest = sURL
httpObject.Open "GET", sRequest, False
httpObject.Send
sGetResult = httpObject.ResponseText

Dim oJSON As Object
Set oJSON = JsonConverter.ParseJson(sGetResult)

  If oJSON.Name = "Ripple" Then
  B1 = oJSON("Ripple")("price_usd")

End If
End Sub

API 调用成功(我相信),但出现语法错误等。希望有人能提供帮助。提前致谢

编辑:这是微软 Excel 2010

编辑 2: 第 16 行和第 17 行(分别是 If oJSON.Name...B1 = oJSON(... 提出了问题,但我一直无法解决 it/find 目前的错误。请参阅 运行 时间错误等的评论

编辑 3:我认为我在第 16 行和第 17 行中引用了 oJSON 而不是项目 (sItem) 时犯了一个错误。但是,即使更改了此设置(例如 If sItem.Name = "Ripple" Then...),它仍然无法正常工作。

编辑 4:我相信我也以错误的方式标记了 excel-cell。我现在不是简单地写 B1 = ...,而是写 Range.("B1").Value = ...,它在测试中有效。

@omegastripes 建议的修改在这里有效。 json对象是字典的集合,所以你需要这样对待它。

Dim oJSON As Object
Set oJSON = JsonConverter.ParseJson(sGetResult)

Dim V As Object
For Each V In oJSON
    If V("name") = "Ripple" Then
        Cells(1, 2) = V("price_usd")
        Exit For
    End If
Next V

看看下面的例子。 JSON.bas 模块导入 VBA 项目进行 JSON 处理。

Option Explicit

Sub Test48852376()

    Dim sJSONString As String
    Dim vJSON As Variant
    Dim sState As String
    Dim vElement As Variant
    Dim sValue As String
    Dim aData()
    Dim aHeader()

    ' Retrieve JSON string
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://api.coinmarketcap.com/v1/ticker/", False
        .Send
        sJSONString = .responseText
    End With
    ' Parse JSON
    JSON.Parse sJSONString, vJSON, sState
    If sState = "Error" Then MsgBox "Invalid JSON string": Exit Sub
    ' Extract ripple price_usd
    Do
        For Each vElement In vJSON
            Select Case False
                Case vElement.Exists("id")
                Case vElement("id") = "ripple"
                Case vElement.Exists("price_usd")
                Case Else
                    MsgBox "ripple price_usd " & vElement("price_usd")
                    Exit Do
            End Select
        Next
        MsgBox "ripple price_usd not found"
    Loop Until True
    ' Output the entire table to the worksheet
    JSON.ToArray vJSON, aData, aHeader
    With Sheets(1)
        .Cells.Delete
        .Cells.WrapText = False
        OutputArray .Cells(1, 1), aHeader
        Output2DArray .Cells(2, 1), aData
        .Columns.AutoFit
    End With
    MsgBox "Completed"

End Sub

Sub OutputArray(oDstRng As Range, aCells As Variant)

    With oDstRng
        .Parent.Select
        With .Resize(1, UBound(aCells) - LBound(aCells) + 1)
            .NumberFormat = "@"
            .Value = aCells
        End With
    End With

End Sub

Sub Output2DArray(oDstRng As Range, aCells As Variant)

    With oDstRng
        .Parent.Select
        With .Resize( _
                UBound(aCells, 1) - LBound(aCells, 1) + 1, _
                UBound(aCells, 2) - LBound(aCells, 2) + 1)
            .NumberFormat = "@"
            .Value = aCells
        End With
    End With

End Sub

我的输出如下:

顺便说一句,应用了类似的方法 in other answers