设置时间转换格式
JSON.registerObjectMarshaller(Date) {
return it?.format("yyyy-MM-dd HH:mm:ss")
}
输出pojo的指定字段
JSON.registerObjectMarshaller(User) {
user ->
return [
id: user.id,
username: user.username
]
}
输出json
render obj as JSON
JSON json = new JSON(obj)
render json
使用 new JSON方式 设置只包含或不包含的字段
JSON json = new JSON(map) //在末尾使用,即map的值不会改变的时候
json.setIncludes(User.class, ["id", "address", "phone", "username"]) //只包含的字段
json.setIncludes(Role.class, ["name"])
json.setExcludes(Img.class, ["memo"]) //不包含的字段
render json
以上内容源自互联网,自己做的简单总结,记录一下。