JSON output of a view in Grails JSON output of a view in Grails json json

JSON output of a view in Grails


Add the following to your controller:

def list = {    params.max = Math.min(params.max ? params.int('max') : 10, 100)    def personList = Person.list(params)    withFormat {        html {            [personInstanceList: personList, personInstanceTotal: Person.count()]        }        json {            render personList as JSON        }    }}

This should support both your scaffolding and the JSON output.

You can access the scaffolding as:

http://localhost:8080/contacts/person/list

You can access the Person list as json with:

http://localhost:8080/contacts/person/list?format=json

There are other ways to do it too, but I like doing it this way to leave the scaffolding around for testing.