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

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

Android Gson.fromJson error -