使用VBJSON迭代嵌套对象

时间:2013-11-18 20:49:25

标签: json excel vba vb6

我正在尝试通过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

下面是我从网上找到的代码拼凑而成的代码,现在它将拉出与行中每个属性相关联的值。但我只需要提取“ Value ”部分的内容,我似乎无法弄清楚如何做到这一点。我想我真的只需要帮助我的for循环,因为我有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

1 个答案:

答案 0 :(得分:2)

遇到类似问题,请在此处查看我的回答: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

(或类似的东西)

相关问题