0

我正在尝试使用 Excel VBA从网站(链接)中抓取内容。这是来自服务器的 .json 响应(Link):

{
  "TopicDetails": {
    "type": 0,
    "ccm2Id": 31088568,
    "cftId": 0,
    "identifier": "FETOPEN-01-2018-2019-2020",
    "title": "FET-Open Challenging Current Thinking",
    "actions": [
      {
        "status": {
          "id": 31094502,
          "abbreviation": "Open",
          "description": "Open"
        },
        "types": [
          "RIA Research and Innovation action"
        ],
        "plannedOpeningDate": "07 November 2017",
        "submissionProcedure": {
          "id": 31094504,
          "abbreviation": "multiple cut-off",
          "description": "multiple cut-off"
        },
        "deadlineDates": [
          "16 May 2018",
          "24 January 2019",
          "18 September 2019",
          "03 June 2020"
        ]
      }
    ]
  }
}

为此,编写了一个宏,效果很好。但是,我在访问存储在块“操作”中的信息(尤其是具有关键“类型”和最新截止日期的数据)时遇到了困难。错误消息是“下标超出范围”。

这是我的代码的相关部分:

Private Sub getJson()

Dim http As Object
Dim JSON As Object
Dim response As String
Dim url As String
Dim id As String
Dim oTarget As Object

id = "FETOPEN-01-2018-2019-2020"
url = "https://ec.europa.eu/info/funding-tenders/opportunities/data/topicDetails/" & LCase(id) & ".json?lang=en"

Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "GET", url, False
http.send
response = http.responseText

Set JSON = JsonConverter.ParseJson(response)

'--- WORKS ---
Cells(5, 11).Value = JSON("TopicDetails")("title")

'--- DOESN'T WORK ---
'--- Test 1 ---
Cells(5, 17).Value = JSON("TopicDetails")("actions")("types")
'--- Test 2 ---
Cells(5, 18).Value = JSON("TopicDetails")("actions")(0)
'--- Test 3 ---
Cells(5, 19).Value = JSON("TopicDetails")("actions")(0)("types")
'--- Test 4 ---
Set oTarget = JSON("TopicDetails")("actions")
With oTarget
    Cells(5, 18).Value = .item(0).item(0)
End With

End Sub

在尝试接近“动作”数组的元素时,我发现以下代码提供 1 作为答案(这是有道理的):

Set oTarget = JSON("TopicDetails")("actions")
Cells(5, 18).Value = oTarget.count

同时,在尝试接近数组的下一个级别时,以下代码提供了一个错误(“下标超出范围”),而不是 5,正如人们所预料的那样:

Set oTarget = JSON("TopicDetails")("actions")(0)
Cells(5, 18).Value = oTarget.count

如何提取信息“RIA Research and Innovation action”(具有关键“类型”)和最新截止日期 2020 年 6 月 3 日(具有关键“deadlineDates”)?

先感谢您!任何帮助深表感谢!

马克西姆

4

1 回答 1

1

这是因为数据类型“类型”是数组。

根据 VBA-JSON 示例https://github.com/VBA-tools/VBA-JSON,数组索引从 1 开始。

尝试这个:

Cells(5, 19).Value = JSON("TopicDetails")("actions")(1)("types")(1)
于 2020-05-25T12:30:30.580 回答