在Excel VBA中解析JSON并按键访问数组
我正在尝试使用Excel VBA从网站()中获取内容。以下是来自服务器()的.json响应: 出于这个原因,编写了一个宏,效果相当不错。但是,我很难访问存储在块“actions”中的信息(尤其是带有键“types”和最新截止日期的数据)。错误消息为“下标超出范围” 以下是我的代码的相关部分:在Excel VBA中解析JSON并按键访问数组,json,vba,web-scraping,jsonconverter,Json,Vba,Web Scraping,Jsonconverter,我正在尝试使用Excel VBA从网站()中获取内容。以下是来自服务器()的.json响应: 出于这个原因,编写了一个宏,效果相当不错。但是,我很难访问存储在块“actions”中的信息(尤其是带有键“types”和最新截止日期的数据)。错误消息为“下标超出范围” 以下是我的代码的相关部分: Private Sub getJson() Dim http As Object Dim JSON As Object Dim response As String Dim url As String D
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
在尝试接近“actions”数组的元素时,我发现以下代码提供了1作为答案(这很有意义):
同时,在尝试接近数组的下一个级别时,以下代码会传递一个错误(“下标超出范围”),而不是如有人所料的5:
Set oTarget = JSON("TopicDetails")("actions")(0)
Cells(5, 18).Value = oTarget.count
如何提取信息“RIA研究与创新行动”(具有关键“类型”)和最新截止日期2020年6月3日(具有关键“截止日期”)
提前谢谢你!非常感谢您的帮助
Maksim这是因为数据类型“types”是数组
根据VBA-JSON示例,数组索引从1开始
试试这个:
Cells(5, 19).Value = JSON("TopicDetails")("actions")(1)("types")(1)
Set oTarget = JSON("TopicDetails")("actions")(0)
Cells(5, 18).Value = oTarget.count
Cells(5, 19).Value = JSON("TopicDetails")("actions")(1)("types")(1)