完成createCriteria查询后,我只有一个对象列表,其中包含7个字符串。我想做的只是获得这七个要素之一?
List<Object> result - com.test.MyService.getConfig(String, String, String, String, String, String, String)
这是我在列表中得到的元素:
[name:John, surname:John city:Rome, car:BMW, country:Italy, day:Monday, color:Red,]
例如,我只想访问以获取字符串“country”才能在方法中返回它:
我正在尝试这样做:
result.get(4)
我开始这个错误:
java.lang.IndexOutOfBoundsException: Index: 4, Size: 1
最佳答案
尝试
def oneObject = YourDomainClass.createCriteria().get{
//add your criteria if any
}
return oneObject.country
之所以获得
java.lang.IndexOutOfBoundsException: Index: 4, Size: 1
,是因为当您执行YourDomainClass.createCriteria().list{}
时,会得到YourDomainClass对象(只有一个对象)的列表。因此,如果要使用YourDomainClass.createCriteria().list{}
,则应执行:def objects = YourDomainClass.createCriteria().list{}
if(objects) //check if any object is in list
return objects[0].country
关于list - 如何在groovy/grails中获取列表中一个元素的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35205451/