如何只发送类的某些属性为json [英] How to only send certain properties of a class as json

查看:112
本文介绍了如何只发送类的某些属性为json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  def showSomething(){
render Color.get( params.id)as JSON
}

这将渲染Color类中的所有属性作为JSON。然而,如果我只想呈现两个属性,比如 colorName和阴影

解决方案

如果你想渲染相同的属性,Gjordis有正确的选择。但是,您可以简单地这样做:
$ b

  Color color = Color.get(params.id)
render([ colorName:color:color:color.shade] as JSON)


I have a simple action such as this:

def showSomething() {
   render Color.get(params.id) as JSON
}

This will render all the properties in the Color class as JSON. However, what if I only want to render two properties, say, colorName and shade?

解决方案

Gjordis has the right option if you want to render the same properties everytime you render the object. However, you could simply do this:

Color color = Color.get(params.id)
render ([colorName: color.colorName, shade: color.shade] as JSON)

这篇关于如何只发送类的某些属性为json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆