2014-11-01 59 views
2

我試圖我Map<String, TableEntry>轉換成JSON在我的控制器如下的Grails:將非域類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':[email protected], 'Team Z':[email protected] 

我如何得到這個完全轉換?

回答

2

您的渲染語句不正確。您有:

render(tables) as JSON 

但是,應該閱讀:

render tables as JSON 

通過包裝括號中的變量「表」,渲染正在發生的事情,然後纔可以施展「表」,以JSON。

+2

我也有這個語法的問題,這取決於表達式的複雜性,所以我通常使用'render(表格作爲JSON)'來確保'as'正確應用 – 2014-11-02 00:45:14