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

前端 未结 2 981
轻奢々
轻奢々 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:22

    Here's a basic approach using the scriptcontrol:

    Sub Tester()
    
        Dim json As String
        Dim sc As Object
        Dim o
    
        Set sc = CreateObject("scriptcontrol")
        sc.Language = "JScript"
    
        json = HttpGet("https://itunes.apple.com/lookup?id=343200656&country=AL")
    
        'some json property names may be keywords in VBA, so replace with
        '  something similar....
        json = Replace(json, """description""", """description_r""")
        Debug.Print json
    
        sc.Eval "var obj=(" & json & ")" 'evaluate the json response
        'add some accessor functions
        sc.AddCode "function getResultCount(){return obj.resultCount;}"
        sc.AddCode "function getResult(i){return obj.results[i];}"
    
        Debug.Print sc.Run("getResultCount")
    
        Set o = sc.Run("getResult", 0)
        Debug.Print o.kind, o.features, o.description_r
    
    End Sub
    
    Function HttpGet(url As String) As String
        Dim oHTML As Object
        Set oHTML = CreateObject("Microsoft.XMLHTTP")
        With oHTML
            .Open "GET", url, False
            .send
            HttpGet = .responsetext
        End With
    End Function
    

    There's a worked-out approach in Codo's answer to this question: Excel VBA: Parsed JSON Object Loop

提交回复
热议问题