grails - create json string list from gorm query -


i have following domain object

class productquantity implements comparable {      static constraints = {     }      integer quantity      static mapping = {         version false     }      static belongsto = product     static hasmany = [products:product]      int compareto(obj) {         quantity.compareto(obj.quantity)     }  } 

i'd return json string looks [50,100,200]

with gorm, there away without having loop object , create set?

i started here

def availablequantities = productquantity.findall() 

any suggestions?

assuming doing controller :

productquantity.list() json 

you can control json in several ways :

1/ register json marshaller in bootstrap.groovy :

class bootstrap {     def init = { servletcontext ->         json.registerobjectmarshaller(productquantity) {             return it.quantity         }     } } 

2/ simple case, add tostring method productquantity class returns quantity property

3/ more complex use cases, can create custom named configurations (if want produce different types of json same domain class). see post.


Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -