0

我的控制器如下所示:

  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 as JSON
    }

 }

我想要的 JSON 应该是这样的:

成功的 JSON

{
   "meta": {
      "status": 200,
      "msg": "OK"
   },
   "response": {
      "color": {
         "colorname": "Red",
         "shade": "light
      }
   }
}

回复失败:

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

问题

如何在返回 json 时修改控制器操作以解决这两种情况?

4

1 回答 1

0

成功响应:

{
"meta": {
  "status": 200,
  "msg": "OK"
},
"response": {
  "color": {
     "colorname": "Red",
     "shade": "light
  }
}
}

采用:

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

对于不成功的响应:

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

采用

 result = [meta: [status: '400', msg: 'wrong'], response:[color:[] ] ]
于 2013-05-22T07:56:43.917 回答