解析 JSON in Excel VBA 并按键访问数组
Parsing JSON in Excel VBA and access array by key
我正在尝试从网站 (Link) using Excel VBA. Here is the .json response from the server (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"
]
}
]
}
}
为此写了个宏,效果还不错。但是,我在访问块 "actions" 中存储的信息时遇到了困难(尤其是具有键 "types" 和最新截止日期的数据)。错误信息是 "Subscript out of range".
这是我的代码的相关部分:
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 作为答案(这是有道理的):
Set oTarget = JSON("TopicDetails")("actions")
Cells(5, 18).Value = oTarget.count
与此同时,在尝试接近数组的下一层时,以下代码提供了一个错误 ("Subscript out of range"),而不是有人预期的 5:
Set oTarget = JSON("TopicDetails")("actions")(0)
Cells(5, 18).Value = oTarget.count
如何提取信息 "RIA Research and Innovation action"(有密钥 "types")和最新截止日期 2020 年 6 月 3 日(有密钥 "deadlineDates")?
提前致谢!非常感谢任何帮助!
马克西姆
因为数据类型"types"是数组
根据 VBA-JSON 示例 https://github.com/VBA-tools/VBA-JSON 数组索引从 1 开始。
试试这个:
Cells(5, 19).Value = JSON("TopicDetails")("actions")(1)("types")(1)
我正在尝试从网站 (Link) using Excel VBA. Here is the .json response from the server (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"
]
}
]
}
}
为此写了个宏,效果还不错。但是,我在访问块 "actions" 中存储的信息时遇到了困难(尤其是具有键 "types" 和最新截止日期的数据)。错误信息是 "Subscript out of range".
这是我的代码的相关部分:
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 作为答案(这是有道理的):
Set oTarget = JSON("TopicDetails")("actions")
Cells(5, 18).Value = oTarget.count
与此同时,在尝试接近数组的下一层时,以下代码提供了一个错误 ("Subscript out of range"),而不是有人预期的 5:
Set oTarget = JSON("TopicDetails")("actions")(0)
Cells(5, 18).Value = oTarget.count
如何提取信息 "RIA Research and Innovation action"(有密钥 "types")和最新截止日期 2020 年 6 月 3 日(有密钥 "deadlineDates")?
提前致谢!非常感谢任何帮助!
马克西姆
因为数据类型"types"是数组
根据 VBA-JSON 示例 https://github.com/VBA-tools/VBA-JSON 数组索引从 1 开始。
试试这个:
Cells(5, 19).Value = JSON("TopicDetails")("actions")(1)("types")(1)