How to marshal an object into JSON (for Ajax) ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to marshal an object into JSON (for Ajax) ?

tommy2012
Guys, I believe that the grails documentation (v 2.1) on JSON (for Ajax) is not valid since it's not working at all.  After extensive search on Google, I found a solution that worked for a simple case.  For example, if person is the only record retrieved from the database, it can be marshalled into JSON as follows:

def response = [
   "firstName" : person.firstName,
   "lastName" : person.lastName,
   "phone"     : person.phone
]

render response as JSON

However, in the event that persons contain multiple records (rows of data), what is the best way to express persons in JSON format ?  Many thanks.
Reply | Threaded
Open this post in threaded view
|

Re: How to marshal an object into JSON (for Ajax) ?

sergiomichels
Assuming that you have a list of persons, you can try this:

List<Person> persons = ...
render persons as JSON


On Mon, Aug 27, 2012 at 5:57 PM, tommy2012 <[hidden email]> wrote:
Guys, I believe that the grails documentation (v 2.1) on JSON (for Ajax) is
not valid since it's not working at all.  After extensive search on Google,
I found a solution that worked for a simple case.  For example, if person is
the only record retrieved from the database, it can be marshalled into JSON
as follows:

def response = [
   "firstName" : person.firstName,
   "lastName" : person.lastName,
   "phone"     : person.phone
]

render response as JSON

However, in the event that persons contain multiple records (rows of data),
what is the best way to express persons in JSON format ?  Many thanks.



--
View this message in context: http://grails.1312388.n4.nabble.com/How-to-marshal-an-object-into-JSON-for-Ajax-tp4633851.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: How to marshal an object into JSON (for Ajax) ?

jason.stlouis
Here is an interesting blog entry on the topic of custom JSON marshalling as well that might help:


On Mon, Aug 27, 2012 at 5:54 PM, Sergio Michels <[hidden email]> wrote:
Assuming that you have a list of persons, you can try this:

List<Person> persons = ...
render persons as JSON


On Mon, Aug 27, 2012 at 5:57 PM, tommy2012 <[hidden email]> wrote:
Guys, I believe that the grails documentation (v 2.1) on JSON (for Ajax) is
not valid since it's not working at all.  After extensive search on Google,
I found a solution that worked for a simple case.  For example, if person is
the only record retrieved from the database, it can be marshalled into JSON
as follows:

def response = [
   "firstName" : person.firstName,
   "lastName" : person.lastName,
   "phone"     : person.phone
]

render response as JSON

However, in the event that persons contain multiple records (rows of data),
what is the best way to express persons in JSON format ?  Many thanks.



--
View this message in context: http://grails.1312388.n4.nabble.com/How-to-marshal-an-object-into-JSON-for-Ajax-tp4633851.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email