Created
February 12, 2012 02:36
-
-
Save zanthrash/1805881 to your computer and use it in GitHub Desktop.
Grails JsonBuilder Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.json.* | |
class Person { | |
String name | |
List addresses = [] | |
} | |
class Address { | |
String address | |
String state | |
} | |
Person p = new Person(name:"Frank") | |
p.addresses << new Address(address: "123 Street", state:"MN") | |
p.addresses << new Address(address: "987 Ave", state:"WI") | |
def builder = new JsonBuilder() | |
def root = builder.people { | |
person { | |
name "${p.name}" | |
addresses( | |
p.addresses.collect{ | |
Address a -> [addr: a.address, state:a.state] | |
} | |
) | |
} | |
} | |
println builder.toPrettyString() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"people": { | |
"person": { | |
"name": "Frank", | |
"addresses": [ | |
{ | |
"addr": "123 Street", | |
"state": "MN" | |
}, | |
{ | |
"addr": "987 Ave", | |
"state": "WI" | |
} | |
] | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<people> | |
<person> | |
<name>Frank</name> | |
<addresses> | |
<address> | |
<addr>123 Street</addr> | |
<state>MN</state> | |
</address> | |
<address> | |
<addr>987 Ave</addr> | |
<state>WI</state> | |
</address> | |
</addresses> | |
</person> | |
</people>people |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment