JSON导入到Excel

是否可以在macros中编写JSON调用?

我想通过API连接获取JSONstring。 看起来问题是Excel希望参数在HTMLstring中传递,但JSON在HTML正文中传递参数。 有任何想法吗?

由于这是VBA,所以我会使用COM来调用xmlhttprequest但是以同步的方式使用它来避免打乱VBA的单线程执行环境。下面是一个示例类,它演示了一个postget请求:

 'BEGIN CLASS syncWebRequest Private Const REQUEST_COMPLETE = 4 Private m_xmlhttp As Object Private m_response As String Private Sub Class_Initialize() Set m_xmlhttp = CreateObject("Microsoft.XMLHTTP") End Sub Private Sub Class_Terminate() Set m_xmlhttp = Nothing End Sub Property Get Response() As String Response = m_response End Property Property Get Status() As Long Status = m_xmlhttp.Status End Property Public Sub AjaxPost(Url As String, Optional postData As String = "") m_xmlhttp.Open "POST", Url, False m_xmlhttp.setRequestHeader "Content-type", "application/x-www-form-urlencoded" m_xmlhttp.setRequestHeader "Content-length", Len(postData) m_xmlhttp.setRequestHeader "Connection", "close" m_xmlhttp.send (postData) If m_xmlhttp.readyState = REQUEST_COMPLETE Then m_response = m_xmlhttp.responseText End If End Sub Public Sub AjaxGet(Url As String) m_xmlhttp.Open "GET", Url, False m_xmlhttp.setRequestHeader "Connection", "close" m_xmlhttp.send If m_xmlhttp.readyState = REQUEST_COMPLETE Then m_response = m_xmlhttp.responseText End If End Sub 'END CLASS syncWebRequest 

所以现在你可以调用上面的方法来返回你服务器的响应:

 Dim request As New syncWebRequest request.ajaxGet "http://localhost/ClientDB/AllClients?format=json" Dim json as string json = request.Response 

这里的问题是我们希望能够以某种方式读取从服务器返回的数据,而不是直接操纵JSONstring。 什么是我的工作是使用VBA-JSON (谷歌代码导出在这里 )COMtypesCollection来处理JSON数组和Dictionary来处理成员及其声明,parsing器工厂方法Parse ,基本上使创build这些字典集合更简单。

所以现在我们可以parsingJSON:

 [{"Name":"test name","Surname":"test surname","Address":{"Street":"test street","Suburb":"test suburb","City":"test city"}}] 

如下所示:

 Set clients = parser.parse(request.Response) For Each client In clients name = client("Name") surname = client("Surname") street = client("Address")("Street") suburb = client("Address")("Suburb") city = client("Address")("City") Next 

这很好,但是如何能够编辑和回发数据呢? 那么还有一个方法toString从上面的[Collection / Dictionary] JSON数据创build一个JSONstring,假设服务器接受JSON。

我为此写了一个.NET Excel-Addin。 这是一个通用的Excel JSON客户端,通过http将任何JSON对象直接stream入Excel。

文档和安装说明可以在这里find: http : //excel-requests.pathio.com/en/master/

这里是GitHub链接: https : //github.com/ZoomerAnalytics/excel-requests