在Excel VBA中使用API 解析JSON对象

问题描述 投票:0回答:1

我有理解Excel VBA: Parsed JSON Object Loop的问题。

我需要一个以下代码的解决方案:

Sub getPricesOnReport()

 Dim url As String: url = "http://statistics.mla.com.au/ReportApi/RunReport?ReportGuid=70587516-e17a-4065-a8aa-e3fe4c512159&FromDate=13%2F03%2F2017&ToDate=18%2F03%2F2017"
 Dim httpRequest As Object: Set httpRequest = CreateObject("MSXML2.XMLHttp")
 Dim httpResponse As Object
 Dim scriptControl As Object: Set scriptControl = createObject("MSScriptControl.ScriptControl")
 Dim XDOM As ListObject

scriptControl.Language = "JScript"

httpRequest.Open "GET", url, False
httpRequest.send

Set httpResponse = scriptControl.eval("(" + httpRequest.responseText + ")")

With Sheets("MLA")

If httpResponse.ResponseStatus <> "OK" Then
    MsgBox "Error in Response"
Else
Cells(3, 2).Value = httpResponse.ResponseDate
Cells(3, 3).Value = httpResponse.ResponseHeader
Cells(3, 4).Value = httpResponse.ResponseStatus
Cells(3, 5).Value = httpResponse.ResponseDisclaimer
'Cells(4, 2).Value = httpResponse.returnValue ' 
End If  
End With
End Sub

我收到代码错误

Cells(4, 2).Value = httpResponse.returnValue

虽然该对象可用。

PFB图片:

如何修改代码以访问数据?

json excel vba excel-vba
1个回答
0
投票

In this case, Capitalization matters!

ReturnValue需要正确地大写。

当你输入ReturnValue时,如果有其他对returnValue的引用,它可能默认为“小r”。 (VBA试图通过纠正你之前输入它的方式来帮助它!)

在VBA编辑器中:

  • 按Ctrl + H.
  • ReturnValueFind What输入Replace With
  • 确保选中Current Project,并且未选中Match Case
  • 点击Replace All

该单词的每次出现都将更改为正确的大小写。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.