正在解析 json :测试存在 json 键

Parsing json : Test is json key existing

我请求 API REST 一个 JIRA 过滤器,因为 Excel 我 return 我的结果是 json 对象。 我正在解析这个对象并尝试显示我的结果(目前在 msgbox 中)但是当 json 键不存在时我遇到了问题!

我的 json 摘录:

{
    "expand":"schema,names",
    "startAt":0,
    "maxResults":500,
    "total":2,
    "issues":[
    {
        "expand":"operations,versionedRepresentations,editmeta,changelog,renderedFields",
        "id":"00001",
        "fields":{
            "components":[
            {
                "id":"01",
                "name":"component_1"
                },
            {
                "id":"02",
                "name":"component_02"
                }
            ]
            }
        },
    {
        "expand":"operations,versionedRepresentations,editmeta,changelog,renderedFields",
        "id":"00002",
        "fields":{
            "components":[
            ]
            }
        },
    ]
    }

如您所见,在我的第一期 (id 00001) 中,我有一个 2 个组件密钥,但在我的第二期 (id 0002) 中,我没有组件密钥,因为此字段在 JIRA 中为空问题。

所以,我的代码的一部分显示了我的结果:

For Each Item In jsonObject("issues")

    issueId = Item("id")
    compoId1 = Item("fields")("components")(1)("id")
    compoId2 = Item("fields")("components")(2)("id")
    i = i + 1

    'PRINT_OF_MY_RESULT

Next

我的问题: 如果我的问题 (00001) 有一个 "component" 值,没关系,我可以 return 我的结果但是......如果我的问题 (00002) 没有结果,我的代码无法定义 compoId 。 ..我的代码崩溃了。

您有简单的解决方案吗?我尝试使用 Exists、isEmpty 等等……但对我来说没有任何结果:(

您可以像这样修改您的解决方案,

For Each Item In jsonObject("issues")
    issueId = Item("id")
    For Each componentItem In jsonObject(Item("fields")("components"))
      If componentItem("id")==1 then
         compoId1 = componentItem("id")
      EndIf
      If componentItem("id")==1 then
         compoId2 = componentItem("id")
      EndIf

    Next
    i = i + 1

    'PRINT_OF_MY_RESULT

Next