首页 > 解决方案 > 无法通过循环访问 JSONSlurper 数组对象

问题描述

我正在尝试使用 Groovy 访问 Jira Rest API。(而且这个问题不是 Jira 特有的。)

连接没有问题;我可以得到我想要的 JSON 数据。

当我尝试遍历数据中的 JSON 数组时;我失败了。但是,如果我尝试在指定索引处访问 JSON 数组的值;我可以得到数据。

String out_json = get("https://jiraserver.intra/rest/api/2/search/?jql=project%20%3D%20TEST%20ORDER%20BY%20key%20ASC&startAt=0&maxResults=1", authString); // It returns the json data
def out = new JsonSlurper().parseText(out_json)

def key = out.issues[0].key // I can access the key this way

for(int i = 0; i < size; i++){
    out.issues[i].key // And it returns null
    break;
}

对于不知道 JSON 的人来说,它是这样的:

{
    "total": 2,
    
    "issues": [
        {
            "key": "TEST-1"
            -- some other key-values --
        },
        {
            "key": "TEST-2"
            -- some other key-values --
        }
    ]
}

我错过了什么?

标签: groovyjirajsonslurper

解决方案


推荐阅读