Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Groovy通过JSON对象调用/循环_Json_Groovy_Treemap - Fatal编程技术网

Groovy通过JSON对象调用/循环

Groovy通过JSON对象调用/循环,json,groovy,treemap,Json,Groovy,Treemap,我使用groovy的RESTClient发出httpget请求,并有一个JSON响应对象,如下所示 { "page": 1, "pages": 1, "count": 4, "items": [{ "id": 291938, "type": email, "folder": "1234", "isDraft": "false", "number": 349, "o

我使用groovy的
RESTClient
发出
httpget
请求,并有一个
JSON
响应对象,如下所示

{   
    "page": 1,
    "pages": 1,
    "count": 4,
    "items": [{
        "id": 291938,
        "type": email,
        "folder": "1234",
        "isDraft": "false",
        "number": 349,
        "owner": {
            "id": 1234,
            "firstName": "Jack",
            "lastName": "Sprout",
            "email": "jack.sprout@gmail.com",
            "phone": null,
            "type": "user"
        },
        "mailbox": {
            "id": 1234,
            "name": "My Mailbox"
      }
    }]
}
我这样设置代码

def Client = new RESTClient('https://api.myapi.net/v1/conversations/' )
helpscout.setHeaders([Authorization: "Basic 1234", Accept: 'application/json'])
def resp = Client.get(contentType: JSON)
def myResponseObject = resp.getData()
我需要能够遍历多个API调用/JSON对象,并将所有响应项存储到它们自己的变量/类中,并根据需要输出它们

我来自python背景,习惯于通过说
responseObject['items']['id']
来调用特定项,但在这种情况下我不能这样做

我的问题是,我如何能够访问其中一个项目,并对其执行任何需要执行的操作/将其存储到变量中

下面是我的调试器输出

As
getData()
返回一个
Map
您可以像这样引用JSON中的字段:

...
def myResponseObject = resp.getData()
def page = myResponseObject.page
def count = myResponseObject.count
println page // prints 1
println count //prints 4

// Print all items
myResponseObject.items.each {
    println "item: $it"
}

// The python example responseObject['items']['id'] in groovy is
responseObject.items[0].id

// To get the first items createdAt field (according to the image)
println responseObject.items[0].createdAt

我尝试了,page返回null,请参见我添加到问题中的屏幕截图根据图像,
myResponseObject
有一个条目名为
item
,尝试如下:
myResponseObject.item.page
,它将项目存储到变量中,那么,如何调用特定项或字段?我不确定调用特定项是什么意思,请举例说明如何处理字段?例如,如何引用myResponseObject.items[1][4]?它的输出应该是2016-08-10T19:17:30ZAre您确定您提供的示例json与端点返回的内容相同吗?呸!更新了调试器中的实际JSON输出,它现在打印出整数如果您将从端点获得的实际JSON作为文本提供,而不是提供调试器的屏幕截图,那么提供一些帮助会容易得多。根据屏幕截图,JSON完全不同