1

我正在尝试从 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 或向新手解释这些框架的用法的方法?

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
4

2 回答 2

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

于 2013-01-18T06:28:34.003 回答
0

我在查询 Salesforce 的 REST API 时遇到了类似的问题,发现处理 JSONScriptControl最终变得难以管理。我使用以下库来解析和转换为 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
于 2014-04-03T13:43:43.837 回答