Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
654 views
in Technique[技术] by (71.8m points)

groovy - Cannot access JSonSlurper array objects via loop

I am trying to access Jira Rest API using Groovy. (And this question is not Jira-specific.)

There is no problem on connection; I can get the JSON data that I want.

When I tried to iterate over a JSON array in the data; I failed. However, if I tried to access the JSON array's value at specified index; I can get the data.

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;
}

For the one who doesn't know the JSON, it is something like:

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

What am I missing?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...