VBA / JIRA / JSON:将新的键/值添加到从JSON

时间:2017-03-14 21:46:21

标签: json excel vba dictionary jira-rest-api

我正在处理一段代码以从JIRA项目中提取问题,然后遍历每个问题以查看它是否已存在于Excel工作表中。对于任何一种结果,我想添加一个新的键值组合,它将基本上标记问题是否存在,例如“存在”:“真实”。

我使用Tim Hall的JSONConverter(VBA-JSON)代码将JSON响应解析为Excel字典。现在我正在努力理解正确的语法,以便将新的键值添加到字典中。

示例JSON:

"issues": [{
      "expand": "operations,editmeta,changelog,transitions,renderedFields",
      "id": "123456789",
      "self": "url",
      "key": "XY-12345",
      "fields": {
            "issuetype": {
                      "self": "url",
                      "id": "1",
                      "description": descrip.",
                      "iconUrl": "url",
                      "name": "Story",
                      "subtask": false
                        },
                },
           },

这就是我想要产生的(如果字典被解析回JSON;请参阅'exists'):

"issues": [{
      "expand": "operations,editmeta,changelog,transitions,renderedFields",
      "id": "123456789",
      "self": "url",
      "key": "XY-12345",
      "exists": "true",
      "fields": {
            "issuetype": {
                      "self": "url",
                      "id": "1",
                      "description": descrip.",
                      "iconUrl": "url",
                      "name": "Story",
                      "subtask": false
                        },
                },
           },

就代码而言,一旦我从JIRA检索到JSON,我就转换为:

Dim oDict as dictionary
Set oDict = ParseJSON(sJSON)

然后我尝试通过循环遍历所有问题将新项添加到字典中:

for n=1 to oDict("issues").count
    If dotfind(oDict("issues")(n)("key"),"r",sht) = 0 Then '//function to search if key exists
        oDict.Add ("issues")(n)("exists"), "false"
    Else
        oDict.Add ("issues")(n)("exists"), "true"
    End if
next n

最后,我希望能够调用以下内容来获取存在的值

Cells(r,c) = oDict("issues")(n)("exists")

2 个答案:

答案 0 :(得分:3)

尝试按如下方式更改代码:

For n = 1 To oDict("issues").Count
    If dotfind(oDict("issues")(n)("key"), "r", sht) = 0 Then '//function to search if key exists
        oDict("issues")(n).Add "exists", "false"
    Else
        oDict("issues")(n).Add "exists", "true"
    End If
Next n

答案 1 :(得分:0)

这对我有用,HTH。

Private Const sJSON As String = "{" & _
    """issues"": [{" & _
        """expand"": ""operations,editmeta,changelog,transitions,renderedFields""," & _
        """id"": ""123456789""," & _
        """self"": ""url""," & _
        """key"": ""XY-12345""," & _
        """fields"": {" & _
            """issuetype"": {" & _
                """self"": ""url""," & _
                """id"": ""1""," & _
                """description"": ""descrip.""," & _
                """iconUrl"": ""url""," & _
                """name"": ""Story""," & _
                """subtask"": ""false""" & _
            "}" & _
        "}" & _
    "}]" & _
"}"

Sub test()
    Dim sht

    Dim oDict As Scripting.Dictionary
    Set oDict = ParseJson(sJSON)

    Dim issue
    For Each issue In oDict("issues")
        If dotfind(issue("key"), "r", sht) = 0 Then '//function to search if key exists
            issue.Add "exists", "false"
        Else
            issue.Add "exists", "true"
        End If
    Next

    Dim result
    result = ConvertToJson(oDict)

    Debug.Print result

    Dim r, c, n
    r = 1
    c = 1
    n = 1
    Cells(r, c) = oDict("issues")(n)("exists") ' Writes false to "A1"

End Sub

Private Function dotfind(a, b, c) As Integer
    dotfind = 0
End Function
  

输出

{
    "issues": [{
        "expand": "operations,editmeta,changelog,transitions,renderedFields",
        "id": "123456789",
        "self": "url",
        "key": "XY-12345",
        "fields": {
            "issuetype": {
                "self": "url",
                "id": "1",
                "description": "descrip.",
                "iconUrl": "url",
                "name": "Story",
                "subtask": "false"
            }
        },
        "exists": "false"
    }]
}

使用JSONLint验证。