用VBJSON遍历嵌套对象

我正在尝试通过VBA连接到SmartSheet API以将内容拖入Excel工作表中。 我发现VBJSON库已经帮了我一些,但我正在努力迭代通过对象和拉特定的值。

我想要访问每个行的“Value”属性的内容,然后对后续行执行相同的操作。 我最大的问题是,我不知道这个VBJSON库如何工作,因为我找不到任何文档,只有几个例子,他们处理相对简单的JSON示例。

期望的输出

第1行第1列内容| 第1行第2列内容
第2行第1列内容| 第2行第2列内容

JSON

{ "id": 1, "name": "Sheet Name", "columns": [ { "id": 1, "index": 0, "title": "Title of Column", "type": "TEXT_NUMBER", "primary": true }, { "id": 2, "index": 1, "title": "Title of Second Column", "type": "TEXT_NUMBER" }, ], "rows": [ { "id": 1, "rowNumber": 1, "cells": [ { "type": "TEXT_NUMBER", "value": "Row 1 Column 1 Content", "columnId": 1, }, { "type": "TEXT_NUMBER", "value": "Row 1 Column 2 Content", "columnId": 2, }, ], "locked": true, "lockedForUser": true, "expanded": true, "createdAt": "2013-10-11T13:43:24-05:00", "modifiedAt": "2013-11-12T15:13:54-06:00" }, { "id": 2276445193037700, "rowNumber": 2, "cells": [ { "type": "TEXT_NUMBER", "value": "row 2 column 1 content", "columnId": 1, }, { "type": "TEXT_NUMBER", "value": "row 2 column 2 content", "columnId": 2, } ] } 

VBJSON库http://www.ediy.co.nz/vbjson-json-parser-library-in-vb6-xidc55680.html

下面是我从网上find的代码拼凑出来的代码,现在它将拉取与该行中每个属性相关联的值。 但我只需要拉取“ 价值 ”部分的内容,我似乎无法弄清楚如何做到这一点。 我想我真的只需要帮助我的循环,因为我有JSON,我有一个似乎工作的图书馆,我只是努力想办法如何组合。

  Dim xmlHttp As Object Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0") xmlHttp.Open "GET", URl, False xmlHttp.setRequestHeader "Content-Type", "text/xml" xmlHttp.send Dim strDiv As String, startVal As Long, endVal As Long strDiv = xmlHttp.ResponseText startVal = InStr(1, strDiv, "rows", vbTextCompare) endVal = InStr(startVal, strDiv, "]", vbTextCompare) strDiv = "{" & Mid(strDiv, startVal - 1, (endVal - startVal) + 2) & "}" Dim JSON As New JSON Dim p As Object Set p = JSON.parse(strDiv) i = 1 For Each Item In p("rows")(1)("cells")(1) Cells(2, i) = p("rows")(1)("cells")(1)(Item) i = i + 1 Next 

进入类似的问题,请参阅我的答案在这里: https : //stackoverflow.com/a/16825736/1240745

这个库对我来说是一个救命的人: https : //github.com/VBA-tools/VBA-JSON (以前是https://code.google.com/p/vba-json/

我在一个我写的用于访问Salesforce,Trello和其他一些库的库中使用它。 (无耻的插件): https : //github.com/VBA-tools/VBA-Web

使用VBA-JSON库,它会涉及如下内容:

 Dim Parsed As Dictionary Set Parsed = JsonConverter.ParseJson(xmlHttp.ResponseText) ' Object -> Dictionary, so Row and Cell: Dictionary Dim Row As Dictionary Dim Cell As Dictionary ' Array -> Collection, so Parsed("rows"): Collection For Each Row In Parsed("rows") For Each Cell In Row("cells") ' Access Dictionary items by key Cells(Row("rowNumber"), Cell("columnId")) = Cell("value") Next Cell Next Row 

(或类似的东西)