VBA/JIRA/JSON:将新的 key/value 添加到从 JSON 解析的字典中
VBA/JIRA/JSON: add new key/value to dictionary parsed from a JSON
我正在编写一段代码以从 JIRA 项目中提取问题,然后遍历每个问题以查看它是否已存在于 Excel sheet 中。对于任何一种结果,我都想添加一个新的键值组合,它基本上会标记问题是否存在,例如"exists": "true".
我正在使用 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
最后,我希望能够调用下面的方法来获取 exists
的值
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
Output
{
"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 验证。
我正在编写一段代码以从 JIRA 项目中提取问题,然后遍历每个问题以查看它是否已存在于 Excel sheet 中。对于任何一种结果,我都想添加一个新的键值组合,它基本上会标记问题是否存在,例如"exists": "true".
我正在使用 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
最后,我希望能够调用下面的方法来获取 exists
的值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
Output
{
"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 验证。