Excel VBA macro using iTunes search API - fastest way to query & parse JSON results

前端 未结 2 982
轻奢々
轻奢々 2021-01-16 07:39

I am trying to build Excel page from iTunes query data. An example for Angry Birds app my query would look like: https://itunes.apple.com/lookup?id=343200656&country=AL

2条回答
  •  一个人的身影
    2021-01-16 08:04

    I had a similar issue with querying Salesforce's REST API and found dealing with JSON through ScriptControl ended up being unmanageable. I used the following library for parsing and converting to JSON and it's worked perfectly for me: https://code.google.com/p/vba-json/.

    Dim JSON As New JSONLib
    Dim Parsed As Object
    
    Set Parsed = JSON.parse(jsonValue)
    Debug.Print Parsed("resultCount")
    Debug.Print Parsed("results")(0)
    

    Using that library, I then wrapped up some of the common functionality for making web requests that I think would help you out: https://github.com/timhall/Excel-REST

    Using these libraries, your code would look something like the following:

    Dim iTunesClient As New RestClient
    iTunesClient.BaseUrl = "https://itunes.apple.com/"
    
    Dim Request As New RestRequest
    Request.Format = json
    Request.Resource = "lookup"
    Request.AddQuerystringParam "id", "343200656"
    Request.AddQuerystringParam "country", "AL"
    
    Dim Response As RestResponse
    Set Response = iTunesClient.Execute(Request)
    
    ' => GET https://itunes.apple.com/lookup?id=343200656&country=AL
    
    If Response.StatusCode = 200 Then
        ' Response.Data contains converted JSON Dictionary/Collection
        Debug.Print "Result Count: " & Response.Data("resultCount")
        Dim i As Integer
        For i = LBound(Response.Data("results")) To UBound(Response.Data("results"))
            Debug.Print "Result " & i & ": " & Response.Data("results")(i)
        Next i
    Else
        Debug.Print "Error: " & Response.Content
    End If
    

提交回复
热议问题