嗨,团队,我是 groovy 的新手,我知道这很简单,但是我得到了 confused.in 我的控制器,我有 2 个值列表。多数民众赞成在显示

Hi Team, am new in groovy, i know that its simple, but I got confused.in my controller i have 2list of values. thats shown like

Ratios:[2,9,3.5]//双值// AND uom:[1,2,3,4]

现在需要像这样通过地图获取这些,

Ratios:[2]`Ratios:[9]`Ratios:[3.5]`

uom:[1]`uom:[2]`uom:[3]`uom:[4]`

最后需要将所有这些关联到一个数组中,LIKE

MyArray[ Ratios:[2]
         Ratios:[9]
         Ratios:[3.5]
         uom:[1]
         uom:[2]
         uom:[3]
         uom:[4]   ]

我怎么会喜欢this.PLEASE帮帮我 我的代码是,

def jsonSlurperjson = new groovy.json.JsonSlurper();
            def jsonData = [];
            def Ratios;
            def UOM;
            def RatioMap=[];

            def Obj = jsonSlurperjson.parseText(params.Selected);

            if (Obj.uom!= null || Obj.uom!= "") {
                UOM= Obj.uom;
            }
            if (Obj.ratio != null || Obj.ratio != "") {
                Ratios = Obj.ratio;
            }
            for(int i=0; i<Ratios.size(); i++) {
                RatioMap.add(Ratios[i]);

            }

您可以收集两个列表来创建地图并将两个列表相加 结果列表得到最终列表

[2,9,3.5].collect{ [Ratios: it] } + [1,2,3,4].collect{ [uom: it] }
// → [[Ratios:2], [Ratios:9], [Ratios:3.5], [uom:1], [uom:2], [uom:3], [uom:4]]