VBA:计算JSON响应字符串中的项目/ trello获取列表中的卡片计数

时间:2016-12-31 14:03:47

标签: json excel vba api trello

我正在使用VBA-web(https://vba-tools.github.io/VBA-Web/)访问trello api,以获取列表中的卡片

[{
    "id": "584e798dd570ae187b293e5b",
    "checkItemStates": null,
    "closed": false,
    "dateLastActivity": "2016-12-30T09:24:57.531Z",
    "desc": "",
    "descData": null,
    "idBoard": "57873ba94794058756fa0a96",
    "idList": "57873bb3a725f734089702b2",
    "idMembersVoted": [],
    "idShort": 90,
    "idAttachmentCover": null,
    "manualCoverAttachment": false,
    "idLabels": ["57873ba984e677fd3683bef8"],
    "name": "card name / other stuff",
    "pos": 1999.9923706054688,
    "shortLink": "izoqvWJk",
    "badges": {
        "votes": 0,
        "viewingMemberVoted": false,
        "subscribed": false,
        "fogbugz": "",
        "checkItems": 0,
        "checkItemsChecked": 0,
        "comments": 0,
        "attachments": 0,
        "description": false,
        "due": "2016-12-26T11:00:00.000Z",
        "dueComplete": false
    },
    "dueComplete": false,
    "due": "2016-12-26T11:00:00.000Z",
    "idChecklists": [],
    "idMembers": ["54f0cc079bf18f2798dda8bd"],
    "labels": [{
        "id": "57873ba984e677fd3683bef8",
        "idBoard": "57873ba94794058756fa0a96",
        "name": "Urgent",
        "color": "red",
        "uses": 14
    }],
    "shortUrl": "https://trello.com/c/vfvfdvdfv",
    "subscribed": false,
    "url": "https://trello.com/c/fdvfdvdfv/cfvdfv"
},
{
    "id": "5832c2fa7f55fe5637d972ea",
    "checkItemStates": null,
    "closed": false,
    "dateLastActivity": "2016-12-30T09:25:09.222Z",
    "desc": "",
    "descData": null,
    "idBoard": "57873ba94794058756fa0a96",
    "idList": "57873bb3a725f734089702b2",
    "idMembersVoted": [],
    "idShort": 80,
    "idAttachmentCover": null,
    "manualCoverAttachment": false,
    "idLabels": ["57873ba984e677fd3683bef6"],
    "name": "other card name",
    "pos": 2023.9922790527344,
    "shortLink": "XhUPgcsD",
    "badges": {
        "votes": 0,
        "viewingMemberVoted": false,
        "subscribed": false,
        "fogbugz": "",
        "checkItems": 0,
        "checkItemsChecked": 0,
        "comments": 0,
        "attachments": 0,
        "description": false,
        "due": "2016-12-30T15:00:00.000Z",
        "dueComplete": false
    },
    "dueComplete": false,
    "due": "2016-12-30T15:00:00.000Z",
    "idChecklists": [],
    "idMembers": ["54fdbe1a8ecdf184596c7c07"],
    "labels": [{
        "id": "57873ba984e677fd3683bef6",
        "idBoard": "57873ba94794058756fa0a96",
        "name": "Medium",
        "color": "yellow",
        "uses": 1
    }],
    "shortUrl": "https://trello.com/c/XhdfvdfvUPgcsD",
    "subscribed": false,
    "url": "https://trello.com/c/XhUPgcsfdvdffvD/
"

我从api收到了正确的JSON回复:

mocha --compilers ts:ts-node/register,tsx:ts-node/register

但我无法正确统计 idList - >我试图通过使用Response.Data(“idList”)获取列表中的卡数。计数

任何信息如何正确地做到这一点?或哪种解析JSON数据的最佳方法?

1 个答案:

答案 0 :(得分:0)

常规

您的JSON未正确关闭。我在最后添加了}]以关闭并放置在activesheet的单元格A1中(因为我没有API信息)。然后,我从单元格中读取它,好像它是响应文本。

<强>过程:

然后我使用JSONConverter从工作表中解析此字符串。这要求您还通过VBE添加对Microsoft Scripting Runtime的引用&gt;工具&gt;引用。

返回的对象是字典的集合。我测试每个字典是否存在idList密钥,如果存在,则将变量itemCount添加1,以跟踪有idLists个变量。

<强>代码:

Public Sub GetInfoFromSheet()
    Dim jsonStr As String, item As Object, json As Object, itemCount As Long
    jsonStr = [A1]
    Set json = JsonConverter.ParseJson(jsonStr)

    For Each item In json                        'collection
        If item.Exists("idList") Then itemCount = itemCount + 1
    Next item
    Debug.Print "idList count: " & itemCount
End Sub