我正在尝试解析网站上的JSON数据。我成功获取了JSON字符串,但我无法解析它。以下代码中抛出异常:
运行时错误424.对象必需
这是我的代码:
' Download string from URL
Public Function DownloadDataFromURL(url As String) As String
Set Myrequest = CreateObject("WinHttp.WinHttpRequest.5.1")
Myrequest.Open "GET", url
Myrequest.send
Dim WebResponse As String
WebResponse = Myrequest.responseText
Cells(1, 1).Value = WebResponse
DownloadDataFromURL = WebResponse
End Function
' Download all cryptocoins in
Public Sub LoadCryptocoins()
Dim Path As String
Dim Data As String
Dim json As Object
Path = "https://api.coinmarketcap.com/v1/ticker/"
Data = DownloadDataFromURL(Path)
Set jp = New JsonParser
Set jo = jp.Decode(Data)
For Each Item In jp.EnumKeys(jo)
MsgBox (Item.GetValue(jo, "id")) 'The exception is thrown here
Next
End Sub
我在这里使用JSON Parser:Parsing JSON in Excel VBA
我可以在此处找到处理的原始JSON数据:https://api.coinmarketcap.com/v1/ticker/
如何以美元获得每个硬币名称和价格?
答案 0 :(得分:2)
尝试以下脚本。它会获取您提到的所有数据。无需外部解析器即可实现此目的:
Sub coinmarketcap_data()
Dim http As New XMLHTTP60, res As Variant
With http
.Open "GET", "https://api.coinmarketcap.com/v1/ticker/", False
.send
res = .responseText
End With
For r = 1 To UBound(Split(res, "id"": """))
Cells(r, 1) = Split(Split(Split(res, "name"": """)(r), """symbol")(0), """")(0)
Cells(r, 2) = Split(Split(Split(res, "price_usd"": """)(r), """price_btc")(0), """")(0)
Next r
End Sub
参考添加到库:
Microsoft XML, v6.0
答案 1 :(得分:1)
从Excel 2010版本开始,您可以下载加载项" Power Query"或者,如果您有2016版本,它默认在Excel中出现为" Get&变换&#34 ;.你可以轻松地提取数据,在你的情况下它将是:
它将打开ETL excel工具(查询编辑器):
Go"转换"
选择"到表"
我没有更改任何参数>确定
7.单击带有两个箭头的角落,然后选择所需的字段。
这是"高级编辑"代码,如果你只想复制和粘贴:
let
Source = Json.Document(Web.Contents("https://api.coinmarketcap.com/v1/ticker/")),
#"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"id", "name", "symbol", "rank", "price_usd", "price_btc", "24h_volume_usd", "market_cap_usd", "available_supply", "total_supply", "max_supply", "percent_change_1h", "percent_change_24h", "percent_change_7d", "last_updated"}, {"Column1.id", "Column1.name", "Column1.symbol", "Column1.rank", "Column1.price_usd", "Column1.price_btc", "Column1.24h_volume_usd", "Column1.market_cap_usd", "Column1.available_supply", "Column1.total_supply", "Column1.max_supply", "Column1.percent_change_1h", "Column1.percent_change_24h", "Column1.percent_change_7d", "Column1.last_updated"})
in
#"Expanded Column1"
答案 2 :(得分:1)
您得到的异常是因为您解析的JSON对象jo
是一个集合,因此您必须首先遍历集合项,然后才能访问每个项的键和值。
我不知道你正在使用什么解析器,所以我将使用VBA-JSON回答:
Public Sub LoadCryptocoins()
Dim Path As String
Dim Data As String
Dim json As Object
Path = "https://api.coinmarketcap.com/v1/ticker/"
Data = DownloadDataFromURL(Path)
Set json = JsonConverter.ParseJson(Data)
For Each Item In json
MsgBox (Item("name") & " $" & Item("price_usd"))
Next
End Sub
现在,使用你的解析器,我想它应该是这样的:
Public Sub LoadCryptocoins()
Dim Path As String
Dim Data As String
Dim json As Object
Path = "https://api.coinmarketcap.com/v1/ticker/"
Data = DownloadDataFromURL(Path)
Set jp = New JsonParser
Set jo = jp.Decode(Data)
For Each Item In jo
MsgBox (Item.GetValue(jo, "name") & " $" & Item.GetValue(jo, "price_usd"))
Next
End Sub
但这是在黑暗中完全盲目的。一旦你发布了一个你正在使用的解析器的链接,我可以更新我的答案。
答案 3 :(得分:0)
使用https://github.com/VBA-tools/VBA-JSON
的简单示例Public Sub LoadCryptocoins()
Dim Path As String
Dim Data As String
Dim jo As Object, o
Dim i As Long
Path = "https://api.coinmarketcap.com/v1/ticker/"
Data = DownloadDataFromURL(Path)
Set jo = json.parse(Data)
For i = 1 To jo.Count
Set o = jo(i)
Debug.Print o("id")
Next i
End Sub
如果JSON响应表示数组,则json.parse
的返回值将为Collection;如果是对象,则为{Scripting Dictionary>。