2016-01-23 138 views
0

我想使用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文本:API的網頁鏈接包含JSON,使用vba導入到excel

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中有多少頁面。 有人可以幫我嗎?

+0

對於API本身的更多信息,http://runescape.wikia.com/wiki/Application_programming_interface –

回答

0

我不會爲此使用InternetExplorer。相反,我會使用XMLHTTP

解析JSONVBA參見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 
+0

我是否只需要複製粘貼整個事情從我的子解析? –

+0

下載ZIP文件。提取'JsonConverter.bas'。使用VBA項目打開「Excel」和VBA編輯器。右鍵單擊Project Explorer中的VBA項目,然後單擊「導入文件...」。瀏覽到JsonConverter.bas文件並導入它。確保你已經通過'Tools'''References'包含了對「Microsoft Scripting Runtime」的引用。 –