VBA/JIRA/JSON:向从JSON解析的字典添加新的键/值

VBA/JIRA/JSON:向从JSON解析的字典添加新的键/值,json,excel,vba,dictionary,jira-rest-api,Json,Excel,Vba,Dictionary,Jira Rest Api,我正在编写一段代码,从JIRA项目中提取问题,然后循环查看每个问题,看看它是否已经存在于Excel工作表中。对于这两种结果,我想添加一个新的键值组合,该组合将本质上标记问题是否存在,例如“存在”:“真” 我正在使用Tim Hall编写的JSONConverter(VBA-JSON)代码将JSON响应解析到Excel字典中。现在我正在努力理解正确的语法,以便将新的键值添加到字典中 JSON示例: "issues": [{ "expand": "operations,editmeta,c

我正在编写一段代码,从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”):

就代码而言,一旦我从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
最后,我希望能够调用下面的函数来获取exists的值

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

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

n=1到oDict(“问题”)的
。计数
如果dotfind(oDict(“issues”)(n)(“key”),“r”,sht)=0,则“//函数用于搜索是否存在key
添加“存在”、“错误”
其他的
添加“存在”、“真实”
如果结束
下一个

这对我很有效,嗯

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
输出


已通过验证。

尝试此操作时会发生什么?我无法运行代码,因为我在“oDict.Add…”编译错误:预期:=“示例JSON打开和关闭大括号不匹配,最好修复此问题,以便为其他人测试。另外,请发布完整的代码,包括
dotfind()
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"
    }]
}