0

I got the grails output as json which looks like following output

{"code":0,"message":"SUCCESS","packet":{"id":"1","name":"unit1"}}

here, i want packet values to be placed in square bracket like following code

{"code":0,"message":"SUCCESS","packet":[{"id":"1","name":"unit1"}]}

what should i do to get the packets in square bracket

1 Answer 1

3

Javascript/JSON [] if used for arrays. And [{}] is array of objects (you need a one-element array, actually).

So, just pass a one-elemetn list/array, like:

def resp = [
  code: 0,
  message: 'SUCCESS',
  packet: [
    [
       id: '1',
       name: 'unit1'
    ]
  ]
render resp as JSON
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.