API Web链接包含Json,使用vba导入到Excel

时间:2016-01-23 15:17:46

标签: json excel vba api excel-vba

我正在尝试使用其API http://services.runescape.com/m=itemdb_rs/api/catalogue/category.json?category=Xhttp://services.runescape.com/m=itemdb_rs/api/catalogue/items.json?category=X&alpha=Y&page=Z获取盛大交易所中runescape所有项目的数据 我可以在Web浏览器中获取json文本,如下所示:

Sub High_Alch()

Dim IE As New InternetExplorer

Dim url As String

Dim url1 As String

Dim url2 As String

Dim url3 As String

url = "services.runescape.com/m=itemdb_rs/api/catalogue/category.json?category="

IE.Visible = True

For i = 0 To 37


    IE.navigate (url + CStr(i))


Next i

End Sub

如何在vba中获取字符串以及如何使用数据?我想使用类别API来了解每个类别中每个字母的项目数量,然后使用它来了解项目API中有多少页面。 有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

我不会使用InternetExplorer。相反,我会使用XMLHTTP

要使用JSON解析VBA,请参阅https://github.com/VBA-tools/VBA-JSON

Sub test()

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

 sURL = "http://services.runescape.com/m=itemdb_rs/api/catalogue/category.json?category="

 For i = 0 To 1 'only category 0 and 1 because of clicking OK in MsgBox ;-)
  sRequest = sURL & i
  httpObject.Open "GET", sRequest, False
  httpObject.send
  sGetResult = httpObject.responseText
  Set oJSON = JsonConverter.ParseJson(sGetResult)
  Set oAlpha = oJSON("alpha")
  For Each oLetter In oAlpha
    sLetter = oLetter("letter")
    lItems = oLetter("items")
    MsgBox "In category " & i & ": Letter " & sLetter & " has " & lItems & " items."
  Next

 Next

End Sub