Grails:将非域类转换为JSON
我正试图在我的控制器中将我的Grails:将非域类转换为JSON,json,grails,Json,Grails,我正试图在我的控制器中将我的Map转换为JSON,如下所示 def index() { // get tables JSON.use('deep') render(tables) as JSON } 我的TableEntry是一个非域类,因为我不希望持久化它 class TableEntry { String teamName Integer gamesPlayed = 0 Integer gamesWon = 0
Map
转换为JSON,如下所示
def index() {
// get tables
JSON.use('deep')
render(tables) as JSON
}
我的TableEntry
是一个非域类,因为我不希望持久化它
class TableEntry {
String teamName
Integer gamesPlayed = 0
Integer gamesWon = 0
Integer gamesDrawn = 0
Integer gamesLost = 0
Integer points = 0
// other methods
但是,在客户端呈现JSON时,我会得到以下结果:
'Team A':TableEntry@3b52fb28, 'Team Z':TableEntry@44e71d85
如何使其完全转换?您的render语句不正确。你有:
render(tables) as JSON
但应改为:
render tables as JSON
通过将变量“tables”包装在括号中,在将“tables”强制转换为JSON之前进行渲染。根据表达式的复杂性,我在该语法方面也遇到了问题,因此我通常使用
render(tables as JSON)
来确保as
正确应用