2

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

最终,我试图获取 Ripple 的价格(第 16 行),然后在我的 Excel 工作表中设置单元格 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调用成功(我相信),但我得到语法错误等。希望有人能够提供帮助。提前致谢

编辑:这是 Microsoft Excel 2010

编辑 2:它是第 16 行和第 17 行(分别If oJSON.Name...B1 = oJSON(...问题所在,但到目前为止我一直无法解决它/找到错误。请参阅运行时错误等的评论。

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

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

4

2 回答 2

4

看看下面的例子。将 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

我的输出如下:

输出

顺便说一句,其他答案中应用的类似方法。

于 2018-02-18T16:41:56.457 回答
3

@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
于 2018-02-18T14:53:48.207 回答