rendering JSON in GRAILS with part of the attributes of an object

Posted by bsreekanth on Stack Overflow See other posts from Stack Overflow or by bsreekanth
Published on 2010-04-01T18:29:34Z Indexed on 2010/04/01 18:33 UTC
Read the original article Hit count: 520

Filed under:
|

Hello, I am trying to build JSON from two fields. Say, I have a list of object(party), and I only need to pass 2 items as JSON pair.

def list = getMyList() //it contains 2 party objects
partyTo = array {
    for (i in list) {
      x partyId: i.id
      y partyName: i.toString()          
    }
  }

The JSON string is

{"partyTo":[
    {"partyId":12},
    {"partyName":"Ar"},
    {"partyId":9},
    {"partyName":"Sr"}
]

}

when I extract it at the client, it is treated as 4 objects. I wanted as 2 objects, with the below format.

{"partyTo":[ {"partyId":12 , "partyName":"Ar"},
    {"partyId":9 , "partyName":"Sr"}
]

}

I'm getting 4 objects, probably because I use an array to build JSON. I'm new to groovy and JSON, so not sure about the right syntax combinations. Any help highly appreciated. thanks.

© Stack Overflow or respective owner

Related posts about grails

Related posts about JSON