【发布时间】:2020-07-02 15:36:25
【问题描述】:
我从 api 得到一个 json 响应并解析它以在 excel 中更新。下面是代码。我无法进一步解析以获取价格信息。
Dim strResult As String
Dim objHTTP As Object
Dim URL As String
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
URL = "https://bitbns.com/order/getTickerAll"
objHTTP.Open "GET", URL, False
objHTTP.Send
Set JSON = JsonConverter.ParseJson(objHTTP.ResponseText)
'strResult = objHTTP.ResponseText
'MsgBox JSON(1)("BTC")("sellPrice")
baseCol = 9
buyCol = 10
sellCol = 11
i = 1
Dim keyCurr As String
For Each Item In JSON
ActiveSheet.Cells(i + 2, baseCol).Value = Item.Keys
i = i + 1
Next
请帮忙。正如您在上面的评论中看到的那样,我能够获取硬编码的数据
MsgBox JSON(1)("BTC")("sellPrice")
但是当我尝试让它循环时,我无法做到。以下是我尝试过但没有成功的方法。
ActiveSheet.Cells(i + 2, baseCol).Value = JSON(i)(Item.Keys)("sellPrice")
ActiveSheet.Cells(i + 2, baseCol).Value = JSON(i)(" + Item.Keys + ")("sellPrice")
ActiveSheet.Cells(i + 2, baseCol).Value = JSON(i)(Item(0))("sellPrice")
ActiveSheet.Cells(i + 2, baseCol).Value = JSON(i)(Item(1))("sellPrice")
为了解析 JSON,我使用 vbaJSON 库。它似乎返回了正确的对象(可以看到能够以硬编码方式访问,但无法循环访问)
更新:根据 Vityata 的提示,以下代码似乎运行良好。感谢大家的即时帮助。 :)
For Each Item In JSON
ActiveSheet.Cells(i + 2, baseCol).Value = Item.Keys
For Each curr In Item
ActiveSheet.Cells(i + 2, buyCol).Value = JSON(i)(curr)("buyPrice")
ActiveSheet.Cells(i + 2, sellCol).Value = JSON(i)(curr)("sellPrice")
i = i + 1
Next curr
Next Item
【问题讨论】: