Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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
Loops 在grails中,我如何在这种情况下使用map?_Loops_Grails_Collections_Groovy_Hashmap - Fatal编程技术网

Loops 在grails中,我如何在这种情况下使用map?

Loops 在grails中,我如何在这种情况下使用map?,loops,grails,collections,groovy,hashmap,Loops,Grails,Collections,Groovy,Hashmap,我需要显示ec2上运行的服务器信息。我已经设法将它们显示为json,但现在我只需要显示某些字段。下面显示了如何显示statusCode和RunType。类似的概念也适用于其他领域 def check = reservations.each { Reservation reservation -> reservation.instances.each() { Instance instance -> def code = instance.s

我需要显示ec2上运行的服务器信息。我已经设法将它们显示为json,但现在我只需要显示某些字段。下面显示了如何显示
statusCode
RunType
。类似的概念也适用于其他领域

  def check = reservations.each { Reservation reservation ->
        reservation.instances.each() { Instance instance ->
            def code = instance.state
            def StatusCode = code.code
//Get other values


            instance.tags.each() { Tag tag5 ->
                def KeyName2 = tag5.key;
                if (KeyName2 == 'RunType') {
                    def RunType = tag5.value;
                }
            }
       }

instance.tags.each() { Tag tag2 ->

                def KeyName2 = tag2.key;

                if (KeyName2 == 'RunType') {
                    def value2 = tag2.value;                    }
            }

            instance.tags.each() { Tag tag3 ->

                def KeyName3 = tag3.key;

                if (KeyName3 == 'StartedBy') {
                    def value = tag3.value;
                }
            } 
我想得到这样的东西,显示在我的gsp页面,并循环它为每台服务器

def map = [StatusCode:"80", RunType:"Always", value"test", value2:"abc"]

但不确定如何在通过代码获取值时向地图中添加值。从代码中,一个状态代码可以有多个
值5
。如果情况并非如此,则您可以执行以下操作:

def myList = []
def check = reservations.each { Reservation reservation ->
                reservation.instances.each() { Instance instance ->
                def statusCode = instance.state?.code
                def value5 = instance.tags?.find{it.key == 'RunType'}?.value
                myList << ["StatusCode": statusCode, "RunType": value5 ]
           }
      }
def myList=[]
def check=保留。每个{保留->
reservation.instances.each(){Instance-Instance->
def statusCode=实例.state?.code
def value5=instance.tags?.find{it.key=='RunType'}?.value

myList您可能可以使用以下功能:

def result = reservations.collectMany { reservation ->
  reservation.instances.collect { instance ->
    [ StatusCode: instance.code,
      RunType   : instance.tags.find { it.key == 'RunType' }?.value,
      StartedBy : instance.tags.find { it.key == 'StartedBy' }?.value ]
  }
}

更新问题

StatusCode和Valuer 5不同字段后更新,我想我把你搞糊涂了。Value5是runtype。你能在问题中显示一个结构化的虚拟映射,说明你想在最后得到什么吗?类似于我创建的映射there@TestUser这些地图的列表?是的,因为大约有50-60台服务器,我想是一个列表地图的种类是我所需要的。你能编辑你的问题来添加你期望得到的地图的形状吗?那么,你想要一个地图列表吗?很难看到你有什么和你想要什么…是的,我想你最终必须使用地图列表。请看下面我的答案。同时更新我的答案;-)如果你仍然对实现持怀疑态度n参考此镜像您的用例。