VBA / JIRA / JSON:将新的键/值添加到从JSONparsing的字典中

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

我使用Tim Hall的JSONConverter(VBA-JSON)代码将JSON响应parsing到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 }, }, }, 

这是我正在试图产生(如果字典被parsing回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") 

尝试更改您的代码,如下所示:

 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 

这对我有用,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" }] } 

用JSONLintvalidation。