歧义表达式可以是无参数的闭包表达式,也可以是孤立的开放代码块;

Ambiguous expression could be either a parameterless closure expression or an isolated open code block;

我有以下代码块,它将创建一个对象,其中包含“basixCertificateNumbers”数组中所有对象的串联字符串。

def object= jsonSlurper.parseText '''
{
    "basixCertificateNumbers": [
        {
          "basixCertificateNumber": "012-012"
        },
        {
            "basixCertificateNumber": "045-123"
          }
      ]
}
'''
def concatdObj = jsonSlurper.parseText '''
{
  "basixNumber" : ""
}
'''

def content = object.each{ entry->
    if(entry.value.getClass().name === "java.util.ArrayList"){
        for (basixIndex = 0 ; basixIndex < entry.value.size(); basixIndex++){
            entry.value[basixIndex].each{ nestedEntry->{
                concatdObj.basixNumber = concatdObj.basixNumber + nestedEntry.value + " "
            }}
        }
        concatdObj.basixNumber = concatdObj.basixNumber.substring(0, concatdObj.basixNumber.length() - 1);
    }}

我目前收到以下错误:

 Ambiguous expression could be either a parameterless closure expression or an isolated open code block;
   solution: Add an explicit closure parameter list, e.g. {it -> ...}, or force it to be treated as an open block by giving it a label, e.g. L:{...} @ line 41, column 56.
   asixIndex].each{ nestedEntry->{
                                 ^

尽管建议的解决方案是将标签贴在上面,但我不确定将其贴在哪里是最佳方式。

目前的解决方案是去掉 nestedEntry 后面的“{”,如下所示:

entry.value[basixIndex].each{ nestedEntry->
                concatdObj.basixNumber = concatdObj.basixNumber + nestedEntry.value + " "
            }

但是,我认为这不是最佳的做事方式,所以如果有人有更好的主意。这将是一个很大的帮助!

我想要的输出是:

{
   "basixNumber" : "012-012 045-123"
}

你可以做到

def content = [
    basixNumber: object.basixCertificateNumbers.basixCertificateNumber.join(' ')
]
String jsonOutput = new JsonOutput().toJson(content)

你不需要concatdObj