Inserted Nested Item in Generated JSON in Groovy/Grails -


i using grails 2.1 render json part of restful api created. domain class, based on sqlserver table, looks this:

string firstname
string lastname
string officialaddress1
string officaladdress2
string preferredaddress1
string preferredaddress2
(etc.). . .

which returns json similar this:

{
"firstname": "joe",
"lastname": "hill",
"officialaddress1": "1100 wob hill",
"officialaddress2": "apt. # 3",
"preferredaddress1": "1100 wobbly lane.",
"preferredaddress2": "apartment 3."
}

it working fine client wants me nest results in fashion:
{
"firstname": "joe",
"lastname": "hill",
preferredaddress {
"preferredaddress1": "1100 wobbly lane.",
"preferredaddress1": "apartment 3."
},
officialaddress {
"officialaddress1": "1100 wob hill",
"officialaddress2": "apt. # 3"
}
}

question since domain class, , database, not structure in way return type of nested result how can change in returned json? have abandon way of regurgitating json based on database/domain object , custom converter of kind?

i'm new stackoverflow thing , hope not mess think know need. in bootstrap.groovy file find "def init = { servletcontext -> " line put in there this:

json.registerobjectmarshaller(yourdomainname) { def returnarray = [:] returnarray['firstname'] = it.firstname returnarray['lastname'] = it.lastname returnarray['preferredaddress'] = [it.preferredaddress1 ,it.preferredaddress2] returnarray['officialaddress'] = [it.officialaddress1 ,it.officialaddress2] return returnarray } 

now when use render json did grails in bootstrap , render domain asked.

hope helps


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -