web services - Building complete JSON in grails -


my controller looks this:

  def save() {     js {        def color = new color(params)        color.save()        def result        if (!color.haserrors())         {           result = [colorname: color.name, colorshde: color.shade]        }        else        {          result = "..."        }        render result json     }   } 

the json desire should this:

successful json

{    "meta": {       "status": 200,       "msg": "ok"    },    "response": {       "color": {          "colorname": "red",          "shade": "light       }    } } 

unsuccessful response:

{    "meta": {       "status": 400,       "msg": "something went worn"    },    "response": {       "color": {       }    } } 

question

how can modify controller action account both scenarios while returning json?

for successful response:

{ "meta": {   "status": 200,   "msg": "ok" }, "response": {   "color": {      "colorname": "red",      "shade": "light   } } } 

use:

result = [meta: [status: '200', msg: 'ok'], response:[color:[colorname: color.name, colorshde: color.shade] ] ] 

for unsuccessful response:

{   "meta": {   "status": 400,   "msg": "something went worn" }, "response": {   "color": {   } } } 

use

 result = [meta: [status: '400', msg: 'wrong'], response:[color:[] ] ] 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -