使用iTunes搜索API的Excel VBA宏 - 查询的最快方式&解析JSON结果

时间:2013-01-18 03:01:21

标签: json excel-vba itunes vba excel

我正在尝试从iTunes查询数据构建Excel页面。 愤怒的小鸟应用程序的示例我的查询将如下所示: https://itunes.apple.com/lookup?id=343200656&country=AL检查阿尔巴尼亚iTunes https://itunes.apple.com/lookup?id=343200656&country=DZ检查阿尔及利亚iTunes ......还有150家商店

我的问题是执行此查询和解析响应的最有效方法。 我只知道如何进行xmlhttp查询。请告诉我更好的方法。 我已经阅读了VB-JSON,Json.net,CDataSet,fastJSON的一些文档,但无法弄清楚如何开始尝试这些工具。任何人有更多的VBA代码示例拉JSON或解释这些框架的使用方式到newb?

Dim innerHTML As Object
Dim myText As String
JsonCheck = ""
Set innerHTML = CreateObject("Microsoft.XMLHTTP")
With innerHTML
    .Open "GET", iTunesAPI_link, False
    .send
    myText = .responsetext
End With
Set innerHTML = Nothing
If InStr(myText, ":0") = 20 Then   'no results found
    result = "Down"
ElseIf InStr(myText, "Your request produced an error.") = 46 Then 'link error
    result = HTMLCheck(human iTunes link)
Else      'found the app
    result = call function which parses myText for desired fields
Endif

2 个答案:

答案 0 :(得分:3)

这是使用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

Codo对这个问题的回答有一个解决方法:Excel VBA: Parsed JSON Object Loop

答案 1 :(得分:0)

我在查询Salesforce的REST API时遇到了类似的问题,发现通过ScriptControl处理JSON最终无法管理。我使用以下库进行解析和转换为JSON,它对我来说非常有用: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)

使用该库,我随后结束了一些用于发出Web请求的常用功能,我认为这些功能可以帮助您:https://github.com/timhall/Excel-REST

使用这些库,您的代码将如下所示:

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