2

我收到以下错误 - groovy.lang.MissingMethodException:没有方法签名:Script64$_run_closure5_closure7_closure8_closure9_closure10_closure11.doCall() 适用于参数类型:(java.lang.String) 值:可能的解决方案:doCall(java.lang.Object , java.lang.Object), isCase(java.lang.Object), isCase(java.lang.Object) 错误在行:

Code -  EDIT
import groovy.xml.*


List tempList = []
List listgenerated = []

def count = 0
for (a in 0..totalCount-1)
{ 

    //nameList and valueList lists will have all the contents added as below   commented pseudo code 
    /*for (b in 0..50) 
    { 
        nameList.add(b,number)     // number is some calculated value
        valueList.add(b,number)
        e.g. nameList=[name1, name2, name3,name4, name5]
             valueList =[val1, val2, val3, , val5]  

             listgenerated should be = [[name1:val1, name2:val2], [name3:val3, name4: , name5:val5]]                          
    } */


        tempList = []

        for (j in count..nameList.size())
        {
                count = j                             
                def nameKey =  nameList[j]
                def value
                if (nameKey != null)
                {
                    value =  valueList[j]
                    tempList << [(nameKey) : value]

                }                               
        }    
            count = count 
            listgenerated.putAt(a,tempList) 
            number = number +1
}   

def process = { binding, element, name ->
  if( element[ name ] instanceof Collection ) {
    element[ name ].each { n ->
      binding."$name"( n )
    }
  }
  else if( element[ name ] ) {
    binding."$name"( element[ name ] )
  }
}

class Form {
  List fields  
}



def list = [[ name:'a', val:'1' ], [ name:'b', val :'2', name2:4, xyz:'abc', pqr:'']]  //Edited list
f = new Form( fields: list )            //Works fine
f = new Form( fields: listgenerated )   //Gives the above error

String xml = XmlUtil.serialize( new StreamingMarkupBuilder().with { builder ->
  builder.bind { binding ->
    data {
      f.fields.each { fields ->
        item {
          fields.each { name, value ->
            process( binding, fields, name )
          }
        }
      }
    }
  }
} )

如果在创建“列表生成”时在值周围添加单引号,它会将其作为字符,并且在打印两个列表时看起来不同。我无法弄清楚到底出了什么问题。任何帮助表示赞赏。谢谢。Ref - Groovy:为具有属性集合的对象集合动态创建 XML

4

1 回答 1

0

我相信,你在哪里:

//some loop to add multiple values to the list
listgenerated << name+":"+value   

你需要做:

//some loop to add multiple values to the list
listgenerated << [ (name): value ]

并将地图添加到列表而不是字符串。很难说,因为您的代码示例不会在没有更改的情况下运行,而且我不知道是否是解决问题的更改

于 2013-04-03T08:03:57.363 回答