【问题标题】:VBA/JIRA/JSON: add new key/value to dictionary parsed from a JSONVBA/JIRA/JSON:将新的键/值添加到从 JSON 解析的字典中
【发布时间】:2017-03-14 21:46:21
【问题描述】:

我正在编写一段代码以从 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;请参阅“存在”):

"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")

【问题讨论】:

  • 当你尝试这样做会发生什么?
  • 我无法运行代码,因为我在 'oDict.Add...' 上遇到错误 "编译错误:预期:="
  • 示例 JSON 大括号和大括号不匹配,最好修复该问题以便为其他人进行测试。也请发布整个代码,包括dotfind()

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


【解决方案1】:

尝试如下更改您的代码:

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

【讨论】:

    【解决方案2】:

    这对我有用,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 验证。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-13
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      相关资源
      最近更新 更多